Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LINQ将xml读入对象列表_Linq - Fatal编程技术网

LINQ将xml读入对象列表

LINQ将xml读入对象列表,linq,Linq,如何使用linq将此xml读入Reportgroup对象列表。 它的语法是什么。 Reportgroup列表中的每个项目都应该包含Report列表您的Reportgroup类似乎缺少与报表关联的方法。无论如何,您可以这样阅读(对类进行一些调整): public class ReportGroup//CamelCase!!! { 公共int Id{get;set;} 公共字符串名称{get;set;} 公共列表报表{get;set;}//需要保存关联的报表 } 公开课报告 { 公共int Id{g

如何使用linq将此xml读入Reportgroup对象列表。 它的语法是什么。
Reportgroup列表中的每个项目都应该包含Report列表

您的
Reportgroup
类似乎缺少与
报表关联的方法。无论如何,您可以这样阅读(对类进行一些调整):

public class ReportGroup//CamelCase!!!
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表报表{get;set;}//需要保存关联的报表
}
公开课报告
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共bool已调度{get;set;}
}
var doc=XDocument.Load(“path/to/file.xml”);
var reportGroups=doc.Element(“报告组”)
.要素(“报告组”)
.选择(rg=>new ReportGroup
{
Id=(int)rg.Attribute(“Id”),
名称=(字符串)rg.Attribute(“名称”),
报告=rg.元素(“报告”)
.选择(r=>新报告
{
Id=(int)r.Attribute(“Id”),
Name=(string)r.Attribute(“Name”),
IsScheduled=(bool)r.Attribute(“IsScheduled”),
}).ToList();
}).ToList();
<?xml version="1.0" encoding="utf-8" ?>
<reportgroups>
  <Reportgroup id="1" name="reportGroup1">
    <report id="1" name="report1" isSheduled="false"></report>
    <report id="2" name="report2"  isSheduled="false"></report>
    <report id="3" name="report3"  isSheduled="false"></report>
  </Reportgroup>
  <Reportgroup id="2" name="reportGrouop2">
    <report id="4" name="report4"  isSheduled="false"></report>
  </Reportgroup>
  <Reportgroup id="3" name="reportGrouop3"></Reportgroup>
</reportgroups>
public class Reportgroup
{
    public int id { get; set; }
    public string name  { get; set; }
}

public class Report
{
    public int id { get; set; }
    public string name { get; set; }
    public bool isSheduled { get; set; }
}
public class ReportGroup // CamelCase!!!
{
    public int Id { get; set; }
    public string Name  { get; set; }
    public List<Report> Reports { get; set; } // need to hold the associated reports
}

public class Report
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsSheduled { get; set; }
}

var doc = XDocument.Load("path/to/file.xml");
var reportGroups = doc.Element("reportgroups")
    .Elements("Reportgroup")
    .Select(rg => new ReportGroup
    {
        Id = (int)rg.Attribute("id"),
        Name = (string)rg.Attribute("name"),
        Reports = rg.Elements("report")
            .Select(r => new Report
            {
                Id = (int)r.Attribute("id"),
                Name = (string)r.Attribute("name"),
                IsScheduled = (bool)r.Attribute("isScheduled"),
            }).ToList();
    }).ToList();