C# XML中的对象数组列表

C# XML中的对象数组列表,c#,xml,linq,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

我有一个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>
我想将xml转换为报表对象列表

我尝试了下面的代码

var resourceList = doc.Descendants("Resorces")
                                    .First()
                                    .Elements("Resource")
                                    .ToList();
我有这样的价值观


如何将其作为对象列表?

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

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方法,以使其与原始方法保持一致,但正如其他人所说,您只需从文档根获取
元素(“资源”)
,基本上您缺少的是将Xml对象转换为
报表的定义对象的部分。这就是你可以做到的:

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方法,以使其与原始方法保持一致,但正如其他人所说,您可以从文档根获取
元素(“资源”)
,您可以使用
元素
方法获取给定元素名称的元素

    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();

选中此项

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

    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();
检查一下这个

你可以通过这个

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();
XDocument doc=XDocument.Load(xmlpath);
列表资源列表=文档子体(“资源”)
.First()
.要素(“资源”)
.Select(报告=>new报告()
{
ResourceId=(int)report.Attribute(“id”),
ResourceName=(字符串)report.Attribute(“name”),
DownloadCount=(int)report.Attribute(“DownloadCount”)
}).ToList();
你可以通过这个

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();
XDocument doc=XDocument.Load(xmlpath);
列表资源列表=文档子体(“资源”)
.First()
.要素(“资源”)
.Select(报告=>new报告()
{
ResourceId=(int)report.Attribute(“id”),
ResourceName=(字符串)report.Attribute(“name”),
DownloadCount=(int)report.Attribute(“DownloadCount”)
}).ToList();
XmlDocument newdoc=newxmldocument();
newdoc.InnerXml=”
";
列表=新列表();
var selectnode=“资源/资源”;
var nodes=newdoc.SelectNodes(selectnode);
foreach(XmlNode节点中的nod节点)
{
字符串id=nod[“id”]。InnerText;
字符串名称=nod[“name”]。InnerText;
字符串downloadcount=nod[“downloadcount”].InnerText;
列表。添加(id);
列表。添加(名称);
列表。添加(下载计数);
}
Console.WriteLine(list.Count);
XmlDocument newdoc=newxmldocument();
newdoc.InnerXml=”
";
列表=新列表();
var selectnode=“资源/资源”;
var nodes=newdoc.SelectNodes(selectnode);
foreach(XmlNode节点中的nod节点)
{
字符串id=nod[“id”]。InnerText;
字符串名称=nod[“name”]。InnerText;
字符串downloadcount=nod[“downloadcount”].InnerText;
列表。添加(id);
列表。添加(名称);
列表。添加(下载计数);
}
Console.WriteLine(list.Count);

这与@GiladGreen的答案完全相同这里每个人的答案几乎相同这与@GiladGreen的答案完全相同这里每个人的答案几乎相同如果这是你仍然可以改变的东西:资源的颜色就是资源如果这是你仍然可以改变的东西:资源的颜色就是资源