如何使用XmlDocument搜索Node-> Node-> Attibute

时间:2016-11-05 01:07:27

标签: c# xmldocument

我的XML看起来像这样。我想得到" NGSPkgTrackingId" 我必须走Node-> Node->属性。 我需要一些帮助

<TrackingID>{06EB4234-8A65-4C28-AD45-DAC87B972437}</TrackingID>
<Documents>
    <Details Weight="1.7950" ZIP="04011" ZIPPlus4="3103" >
        <Identifier Qualifier="eVSBarcode" Value="4200401192458927004050120118829995" />
        <Identifier Qualifier="REFERENCENUM" Value="301113159600798" />
        <Identifier Qualifier="NGSPkgTrackingId" Value="00983482428">   
    </Details>
    <Details Weight="3.3450" ZIP="04011" CountryCode="US"  >
      < Identifier Qualifier="eVSBarcode" Value="4200401192612927004646230017808858" />
      <Identifier Qualifier="REFERENCENUM" Value="117913788" /> Default="true" />
    </Details>

谢谢

2 个答案:

答案 0 :(得分:1)

假设你有一个像这样的有效xml

<?xml version="1.0" encoding="utf-8" ?> 
<Documents>
    <Details Weight="1.7950" ZIP="04011" ZIPPlus4="3103" >
        <Identifier Qualifier="eVSBarcode" Value="4200401192458927004050120118829995" />
        <Identifier Qualifier="REFERENCENUM" Value="301113159600798" />
        <Identifier Qualifier="NGSPkgTrackingId" Value="00983482428"/>   
    </Details>
    <Details Weight="3.3450" ZIP="04011" CountryCode="US">
      <Identifier Qualifier="eVSBarcode" Value="4200401192612927004646230017808858" />
      <Identifier Qualifier="REFERENCENUM" Value="117913788" /> Default="true" />
    </Details>
</Documents>
  

访问值&#34; NGSPkgTrackingId&#34;使用下面的代码

应该非常简单
            XmlDocument xmlDoc = new XmlDocument();   
            XmlReader reader = XmlReader.Create("C:/Users/.../documents.xml ");
            xmlDoc.Load(reader);
            XmlNodeList nodeList = xmlDoc.SelectNodes("/Documents").Item(0).ChildNodes;
            string val = xmlDoc.DocumentElement.ChildNodes[0].LastChild.Attributes.Item(0).Value;
            Console.WriteLine(val);
  

或者,您可以使用selectNodes

        var node2 =xmlDoc.DocumentElement.SelectNodes(".//Details/Identifier");
        var ans1=  node2.Item(2).Attributes["Qualifier"].Value;
        Console.WriteLine(ans1);
  

第三个选项

            var value= xmlDoc.DocumentElement.SelectSingleNode("descendant::Details").LastChild.Attributes["Qualifier"].Value;
            Console.WriteLine(value);

答案 1 :(得分:1)

尝试查找msdn documentation on XmlDocument。正如har07指出的那样,您可以使用 SelectSingleNode()。你可以参考here

至于代码的外观,假设您只想找到与给定标识符限定符相关联的值,它可以像这样工作:

    public static string FindValue(string qualifier, string xml)
    {
        var value = string.Empty;
        XmlDocument doc = new XmlDocument();
        doc.Load(xml);

        XmlNode root = doc.DocumentElement;

        XmlNode identifier = root.SelectSingleNode(@"descendant::Details/Identifier [@Qualifier='" + qualifier +"']");

        value = identifier.Attributes["Value"].Value;

        return value;
    }

我们应该进行空引用检查,所以我将它们添加到

    public static string FindValue(string qualifier, string xml)
    {
        var value = string.Empty;
        XmlDocument doc = new XmlDocument();
        doc.Load(xml);

        XmlNode identifier = null;
        XmlNode root = doc.DocumentElement;

        if (root != null)
            identifier = root.SelectSingleNode(@"descendant::Details/Identifier [@Qualifier='" + qualifier +"']");

        if (identifier?.Attributes != null) value = identifier.Attributes["Value"].Value;

        return value;
    }