简体   繁体   English

如何获取XML元素属性值?

[英]How to get the XML Element Attribute values?

I have below xml from which I am trying to get the value of HotelCrossRef Element ResponseHotelCode attribute. 我在下面的xml中,尝试从中获取HotelCrossRef元素ResponseHotelCode属性的值。

I have tried below code but I am getting 0 Count in XmlNodeList 我试过下面的代码,但在XmlNodeList得到0计数

string xmlResp = @"<?xml version=""1.0"" encoding=""utf-8""?><OTA_HotelDescriptiveContentNotifRS xmlns=""http://www.opentravel.org/OTA/2003/05"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://www.opentravel.org/OTA/2003/05 OTA_HotelDescriptiveContentNotifRS.xsd"" TimeStamp=""2015-07-31T12:36:23-00:00"" Target=""Test"" Version=""3.000"">
<UniqueID Type=""10"" ID=""1460495"" />
<TPA_Extensions>
<HotelCrossRefs>
<HotelCrossRef RequestHotelCode=""101010"" ResponseHotelCode=""1460495"" />
</HotelCrossRefs>
</TPA_Extensions>
<Success />
</OTA_HotelDescriptiveContentNotifRS>";

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlResp);

XmlNodeList xnList = xmlDocument.SelectNodes("/OTA_HotelDescriptiveContentNotifRS/TPA_Extensions/HotelCrossRefs");
foreach (XmlNode xn in xnList)
{
    if (xn.HasChildNodes)
    {
        foreach (XmlNode childNode in xn.ChildNodes)
        {
            string id = childNode.Attributes["ResponseHotelCode"].Value; 
            Console.WriteLine(id);
        }
    }
}

You have a namespace problem, your xml has a default namespace that you must declare when selecting nodes. 您有一个名称空间问题,您的xml具有一个默认的名称空间,在选择节点时必须声明它。 try the following: 尝试以下方法:

string xmlResp = @"<?xml version=""1.0"" encoding=""utf-8""?><OTA_HotelDescriptiveContentNotifRS xmlns=""http://www.opentravel.org/OTA/2003/05"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://www.opentravel.org/OTA/2003/05 OTA_HotelDescriptiveContentNotifRS.xsd"" TimeStamp=""2015-07-31T12:36:23-00:00"" Target=""Test"" Version=""3.000"">
    <UniqueID Type=""10"" ID=""1460495"" />
    <TPA_Extensions>
    <HotelCrossRefs>
    <HotelCrossRef RequestHotelCode=""101010"" ResponseHotelCode=""1460495"" />
    </HotelCrossRefs>
    </TPA_Extensions>
    <Success />
    </OTA_HotelDescriptiveContentNotifRS>";

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlResp);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
nsmgr.AddNamespace("xn", "http://www.opentravel.org/OTA/2003/05");

XmlNodeList xnList = xmlDocument.SelectNodes("/xn:OTA_HotelDescriptiveContentNotifRS/xn:TPA_Extensions/xn:HotelCrossRefs", nsmgr);
foreach (XmlNode xn in xnList)
{
    if (xn.HasChildNodes)
    {
        foreach (XmlNode childNode in xn.ChildNodes)
        {
            string id = childNode.Attributes["ResponseHotelCode"].Value;
            Console.WriteLine(id);
        }
    }
}

Note that I added another parameter in the SelectNodes method 请注意,我在SelectNodes方法中添加了另一个参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM