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

How to search Node->Node->Attibute using XmlDocument

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

<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>

谢谢

假设您有这样一个有效的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>

Accessing the value "NGSPkgTrackingId" should be pretty straightforward using the below code

            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);

Alternatively, you can use selectNodes

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

Third Option

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

尝试查找 msdn documentation on XmlDocument. As har07 pointed out, you can use SelectSingleNode(). Which you can reference 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;
    }