XmlDocument-如何从多个节点获取所有值

时间:2018-11-20 14:53:01

标签: c# xml xmldocument

这是我的示例XML文件:

<Main>
    <Person>
        <Name>Božena</Name>
        <Surname>Němcová</Surname>
        <Gender>Female</Gender>
        <OrderNum>18</OrderNum>
        <BirthDate>04.02.1820</BirthDate>
    </Person>
    <Person>
        <Name>Jan</Name>
        <Surname>Žižka</Surname>
        <Gender>Male</Gender>
        <OrderNum>7</OrderNum>
        <BirthDate>19.09.1360</BirthDate>
    </Person>
    <Person>
        <Name>Che</Name>
        <Surname>Guevara</Surname>
        <Gender>Male</Gender>
        <OrderNum>27</OrderNum>
        <BirthDate>14.06.1928</BirthDate>
    </Person>
    <Person>
        <Name>Antonie</Name>
        <Surname>de Saint-Exupéry</Surname>
        <Gender>Male</Gender>
        <OrderNum>15</OrderNum>
        <BirthDate>29.06.1900</BirthDate>
    </Person>
</Main>

这是我想用来获取 Name 元素的所有值的列表的代码:

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load("PersonWrite.xml");
        XmlNodeList data = xmlDoc.SelectNodes("Main/Person/Name");

问题是我只能从第一个 Person 元素中获取一个值。

2 个答案:

答案 0 :(得分:2)

我喜欢使用反序列化,使用起来更容易。

using System;
using System.Xml.Serialization;
using System.IO;

public class Main
{
    [XmlElement("Person")]
    public Person[] People { get; set; }
}

public class Person
{
    public string Name { get; set; }

    public string Surname { get; set; }

    public string Gender { get; set; }

    public int OrderNum { get; set; }

    public string BirthDate { get; set; }
}

public class Program
{
    public static void Main()
    {
        var xmlString = @"<Main>
                        <Person>
                            <Name>Božena</Name>
                            <Surname>Němcová</Surname>
                            <Gender>Female</Gender>
                            <OrderNum>18</OrderNum>
                            <BirthDate>04.02.1820</BirthDate>
                        </Person>
                        <Person>
                            <Name>Jan</Name>
                            <Surname>Žižka</Surname>
                            <Gender>Male</Gender>
                            <OrderNum>7</OrderNum>
                            <BirthDate>19.09.1360</BirthDate>
                        </Person>
                        <Person>
                            <Name>Che</Name>
                            <Surname>Guevara</Surname>
                            <Gender>Male</Gender>
                            <OrderNum>27</OrderNum>
                            <BirthDate>14.06.1928</BirthDate>
                        </Person>
                        <Person>
                            <Name>Antonie</Name>
                            <Surname>de Saint-Exupéry</Surname>
                            <Gender>Male</Gender>
                            <OrderNum>15</OrderNum>
                            <BirthDate>29.06.1900</BirthDate>
                        </Person>
                    </Main>";

        var serializer = new XmlSerializer(typeof (Main));
        Main main = null;

        using (var reader = new StringReader(xmlString))
        {
            main = (Main)serializer.Deserialize(reader);
        }

        if (main == null)
        {
            return; 
        }

        Console.WriteLine(main.People.Length);
    }
}

输出:

4

答案 1 :(得分:0)

我将遍历主节点的子节点

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("PersonWrite.xml");
    XmlNodeList root = xmlDoc.SelectNodes("Main");
    foreach (XmlNode xnode in root.ChildNodes)
    {
       //get data from xnode
    }