XML中的对象数组列表

时间:2016-07-26 05:29:15

标签: c# xml linq

我有一个看起来像这样的xml

<?xml version="1.0" encoding="utf-8" ?> 
<Resorces>
<Resource id="3" name="loreum ipsum" downloadcount="5"></Resource>
<Resource id="2" name="loreum ipsum" downloadcount="9"></Resource>
</Resorces>

我有一个班级

public class Report
{
    public int ResourceId {get; set; }
    public string ResourceName { get; set; }
    public int DownloadCount { get; set; }
}

我想将该xml转换为Report对象列表

我尝试了以下代码

var resourceList = doc.Descendants("Resorces")
                                    .First()
                                    .Elements("Resource")
                                    .ToList();

我得到这样的价值,

enter image description here

如何将其作为对象列表?

4 个答案:

答案 0 :(得分:2)

基本上,您缺少的是将Xml对象转换为Report的已定义对象的部分。这就是你如何做到的:

var resourceList = doc.Descendants("Resorces")
                       .First()
                       .Elements("Resource")
                       .Select(element => new Report()
                                  {
                                    ResourceId = (int)element.Attribute("id"),
                                    ResourceName = (string)element.Attribute("name"),
                                    DownloadCount = (int)element.Attribute("downloadcount")
                                  })
                       .ToList();

我保留了之前调用的linq方法,使其保持接近原始状态,但正如其他人所说,您可以从doc根获取Elements("Resource")

答案 1 :(得分:2)

 XmlDocument newdoc = new XmlDocument();
newdoc.InnerXml = " <?xml version="1.0" encoding="utf-8" ?> 
<Resorces>
<Resource id="3" name="loreum ipsum" downloadcount="5"></Resource>
<Resource id="2" name="loreum ipsum" downloadcount="9"></Resource>
</Resorces>";
List<string> list = new List <string>();
var selectnode = "Resorces/Resource";
var nodes = newdoc.SelectNodes(selectnode);
foreach (XmlNode nod in nodes)
{
    string id = nod["id"].InnerText;
    string name    = nod["name"].InnerText;
    string downloadcount = nod["downloadcount"].InnerText;       
    list.Add(id);
    list.Add(name);
    list.Add(downloadcount);
}
Console.WriteLine(list.Count);

答案 2 :(得分:1)

您可以使用Elements方法获取给定元素名称的元素。

    XDocument doc = XDocument.Load(filepath);

    var result = doc.Root
       .Elements("Resource")
       .Select(x=> new Report()
                {
                    ResourceId = int.Parse( x.Attribute("id").Value),
                    ResourceName = (string)x.Attribute("name").Value,
                    DownloadCount = int.Parse( x.Attribute("downloadcount").Value)

                })
        .ToList();

选中此Demo

答案 3 :(得分:1)

你可以通过这个

XDocument doc = XDocument.Load(xmlpath);

List<Report> resourceList = doc.Descendants("Resorces")
                                    .First()
                                    .Elements("Resource")
                                    .Select(report => new Report()
                                                          {
                                        ResourceId = (int)report.Attribute("id"),
                                        ResourceName = (string)report.Attribute("name"),
                                        DownloadCount = (int)report.Attribute("downloadcount")
                                    }).ToList();