2

因此,如果我的 XML 看起来像这样......

<people>
    <person>
        <name>a</name>
    </person>
    <person>
        <name>b</name>
    </person>
</people>

将其解析为名为“people”的 C# 数组的最佳/最简单方法是什么,其中 people[0] 是第一人称对象,然后将如何格式化以及如何访问它?

谢谢!

4

6 回答 6

4

您可以使用LINQ-To-Xml将此文件加载到数组中。

要在加载对象后简单地处理对象,您可以创建一个代表人的类:

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

XElement.Load然后使用-method加载文件:

var document = XElement.Load("persons.xml");
var persons = document.Elements("Person")
    .Select(p => new Person{ Name = p.Element("Name").Value }
    .ToArray();
于 2013-02-06T22:59:03.940 回答
2

我的 C# 生锈了,但这很简单,使用XML 序列化

反序列化(读取),修改,然后序列化(写入):

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

namespace ConsoleApplication1
{

    [XmlRoot("people")]
    public class People
    {
        [XmlElement("person")]
        public Person[] person { get; set; }
    }

    [Serializable]
    public class Person
    {
        [XmlElement("name")]
        public string Name { get; set; }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            People people = null;
            XmlSerializer serializer = new XmlSerializer(typeof(People));
            using (StreamReader reader = new StreamReader("people.xml"))
            {
                people = (People)serializer.Deserialize(reader);
            }
            people.person[0].Name = "Dan";
            using (StreamWriter writer = new StreamWriter("people.xml"))
            {
                serializer.Serialize(writer, people);
            }
        }
    }
}
于 2013-02-06T23:49:38.790 回答
1

您可以使用LinqToXml轻松完成:

var doc = XDocument.Parse(myXmlString); // .Load("filepath");
var persons = doc.Root
                 .Elements("Person")
                 .Select(x=> new Person {Name= x.Element("Name").Value})
                 .ToArray();

它将返回一个Person定义如下的 ' 数组。

public class Person{
     public string Name {get; set;}
}
于 2013-02-06T22:56:56.117 回答
0

假设:

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

然后(查询语法):

var arr = (from p in XDocument.Load(path) // or .Parse(str)
                              .Root
                              .Elements("person")
           select new Person
           {
               Name = (string)p.Attribute("name")
           }).ToArray();

扩展方法语法相同:

XDocument.Load(path)
         .Root
         .Elements("person")
         .Select(p => new new Person
             {
                 Name = (string)p.Attribute("name")
             })
         .ToArray();
于 2013-02-06T22:57:20.057 回答
0
var doc = XDocument.Parse(input);
string[] names = doc.Root.Descendants("name").Select(x => x.Value).ToArray();

如果输入的 xml 格式与您提供的上述语句一样简单,则已足够,否则添加此 where 子句以不捕获namexml 文件中的其他元素:

string[] names = doc.Root.Descendants("name")
                        .Where(x => x.Parent.Name == "person")
                        .Select(x => x.Value).ToArray();
于 2013-02-06T23:00:03.397 回答
0

一条线就够了。

var people=XDocument.Load(path).Root.Elements().Select(y => y.Elements().ToDictionary(x => x.Name, x => x.Value)).ToArray();

您需要指定以下命名空间进行测试

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

测试代码

var path=@"c:\people.xml";
var people=XDocument.Load(path).Root.Elements().Select(y => y.Elements().ToDictionary(x => x.Name, x => x.Value)).ToArray();

foreach(var person in people) {
    Console.WriteLine("name = {0}", person["name"]);
    Console.WriteLine("name = {0}", person["age"]); // requires person have a age defined in your xml file
}

用于测试的示例 xml

<people>
    <person>
        <name>Humbert Humbert</name>
        <age>36</age>
    </person>

    <person>
        <name>Lolita</name>
        <age>12</age>
    </person>
</people>
于 2013-02-06T23:18:37.463 回答