将XML加载到列表中

时间:2014-02-20 18:29:16

标签: c# xml list load

我有一个将List中的数据保存到XML文件的函数。我的问题是,如何将XML文件实际加载到List?

我的XML文件:

<Customers>
<customer ID="0" firstname="xx" lastname="xx" />
</Customers>

1 个答案:

答案 0 :(得分:1)

using System;
using System.Linq;
using System.Xml.Linq;

public class XmlToList
{
    static void Main()
    {
        string xml = "<Customers><customer ID=\"0\" firstname=\"xx\" lastname=\"xx\" />/Customers>";

        XDocument doc = XDocument.Parse(xml);

        var list = doc.Root.Elements("Customers")
                       .Select(node => node.Value)
                       .ToList();

        foreach (string item in list)
        {
            Console.WriteLine(item);
        }
    }
}

你可以参考这篇文章: https://stackoverflow.com/a/956777/3232673