如何从XmlNode对象寻址子节点?

时间:2019-06-22 07:24:48

标签: c# xml xml-parsing

这是我的测试XML文档:

<?xml version="1.0" encoding="utf-8" ?>
<foos>
  <foo>
    <bar>
      <bazs>
        <baz>baz1</baz>
        <baz>baz2</baz>
        <baz>baz3</baz>
      </bazs>
    </bar>
  </foo>
  <!--there will be other foo nodes with same structure but different number of baz nodes-->
</foos>

在本文档中,每个<foo>节点都有一个<bar>节点,每个<bar>节点都有<baz>节点的列表。我想从每个<baz>到达这些<bar>节点。这是我的代码:

using System;
using System.Xml;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("test.xml");
            XmlNodeList fooNodes = xmlDocument.SelectNodes("/foos/foo");
            foreach(XmlNode fooNode in fooNodes)
            {
                XmlNode barNode = fooNode.SelectSingleNode("bar");

                var bazNodes1 = fooNode.SelectNodes("/bar/bazs/baz");

                var bazNodes2 = fooNode.SelectNodes("bar/bazs/baz");

                var bazNodes3 = barNode.SelectNodes("/bazs/baz");

                Console.WriteLine($"Method 1 returned {bazNodes1.Count} nodes.");
                Console.WriteLine($"Method 2 returned {bazNodes2.Count} nodes.");
                Console.WriteLine($"Method 3 returned {bazNodes3.Count} nodes.");
            }
            Console.Read();
        }
    }
}

,会产生:

Method 1 returned 0 nodes.
Method 2 returned 3 nodes.
Method 3 returned 0 nodes.

在这里,我对寻址节点时使用/感到有些困惑。从文档根源获取fooNodes时以/foos/foo的身份与xpath一起工作,但是我无法从{{1}中使用bazNodes来获取/bar/bazs/baz }节点,这对我来说很奇怪。而且我还坚持从<foo>节点获取bazNodes,这是因为我不知道正确的语法。所以我的问题是:

  1. 在寻址节点时,何时应包括前导<bar>
  2. 在此示例中如何从/节点中获得<baz>个节点?

2 个答案:

答案 0 :(得分:1)

您可以使用xml序列化程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Foos));
            Foos foos = (Foos)serializer.Deserialize(reader);
        }
    }
    [XmlRoot(ElementName = "foos")]
    public class Foos
    {
        [XmlElement("foo")]
        public List<Foo> foo { get; set; }  
    }
    [XmlRoot("foo")]
    public class Foo
    {
        [XmlElement("bar")]
        public Bar bar { get; set; }
    }
    [XmlRoot("bar")]
    public class Bar
    {
        [XmlArray("bazs")]
        [XmlArrayItem("baz")]
        public List<string> baz { get; set; }
    }

}

答案 1 :(得分:1)

第一个问题的答案是,如果要启动从根节点中选择的绝对路径,则需要添加前导“ /”。您第二个问题的答案请参考下面的示例代码。另外,我推荐article供参考。

using System;
using System.Xml;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var xmlDoc = new XmlDocument();
            xmlDoc.Load("test.xml");

            XmlNodeList fooNodes1 = xmlDoc.SelectNodes("/foos/foo/bar/bazs/*"); // starts an absolute path that selects from the root
                                                                            // foos element.
            Console.WriteLine($"Method 1 returned {fooNodes1.Count} nodes."); 

            XmlNodeList fooNodes2 = xmlDoc.SelectNodes("/foos/foo");    // "/foo" the document element(<foo>) of this document.

            foreach (XmlNode fooNode in fooNodes2)
            { 
                var bazNodes2 = fooNode.SelectNodes("./bar/bazs/baz");  // "." indicates the current node.
                var bazNodes3 = fooNode.SelectNodes("bar/bazs/baz"); // selects all baz elements that are children of an bazs element, 
                                                                 // which is itself a child of the root bar element.
                var bazNodes4 = fooNode.SelectNodes("bar/bazs/*");   // "*" selects any element in the path.
                var bazNodes5 = fooNode.SelectNodes("bar/*/baz");   
                var bazNodes6 = fooNode.SelectNodes("//bazs/*");     // selects any elements that are children of an bazs element, 
                                                                 // regardless of where bazs appear in the current context.
                var bazNodes7 = fooNode.SelectNodes("bar//baz");     // selects all the baz elements that are under an bar element, 
                                                                 // regardless of where they appear in the current context.
                var bazNodes8 = fooNode.SelectNodes("//bazs/baz");   // selects all the bazs elements that are children of an baz element, 
                                                                 // regardless of where they appear in the document.
                var bazNodes9 = fooNode.SelectNodes("//baz");        // starts a relative path that selects baz element anywhere.

                XmlNode barNode = fooNode.SelectSingleNode("bar");

                var bazNodes10 = barNode.SelectNodes("bazs/*");     // selects all nodes which are contained by a root bazs element.

                Console.WriteLine($"Method 2 returned {bazNodes2.Count} nodes.");
                Console.WriteLine($"Method 3 returned {bazNodes3.Count} nodes.");
                Console.WriteLine($"Method 4 returned {bazNodes4.Count} nodes.");
                Console.WriteLine($"Method 5 returned {bazNodes5.Count} nodes.");
                Console.WriteLine($"Method 6 returned {bazNodes6.Count} nodes.");
                Console.WriteLine($"Method 7 returned {bazNodes7.Count} nodes.");
                Console.WriteLine($"Method 8 returned {bazNodes8.Count} nodes.");
                Console.WriteLine($"Method 9 returned {bazNodes9.Count} nodes.");
                Console.WriteLine($"Method 10 returned {bazNodes10.Count} nodes.");
            }

            Console.Read();
        }
    }
}