如何一次读取多个节点?
How to read many nodes for one time?
我有一个 class:
class Employee
{
public int? ID { get; set; }
public int? EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int? ChiefID { get; set; }
public string ChiefName { get; set; }
public List<int> ListOfEmployees { get; set; }
}
而xml是:
<?xml version="1.0" encoding="UTF8"?>
<Employee>
<Id>1</Id>
<EmloyeeId>1</EmployeeId>
<EmIoyeeName>Bob</EmIoyeeName>
<ChiefID></ChiefID>
<ChiefName></ChiefName>
<Dependent>
<idDependent>2</idDependent>
<idDependent>3</idDependent>
<idDependent>4</idDependent>
<idDependent>5</idDependent>
</Dependent>
</Employee>
</Employees>
我的 C# 代码是:
XDocument xDoc=LoadXML();
IEnumerable<Employee> sourceEmployee=xDoc.Descendants("Employee").Select(d=>new Employee {
ID=(int?)d.Element("Id"),
EmployeeID=(int?)d.Element("EmloyeeId"),
ListOfEmloyees=(List<int>)d.Element("Dependent").Elements("idDependent")//cannot populate ListOfEmloyees
});
但是,我无法将数据读取到 ListOfEmployes 属性。是否可以读取 "Dependent" 节点的所有后代值?
将 XElement
转换为 int
然后调用 .ToList()
:
ListOfEmloyees = d.Elements("Dependent")
.Elements("idDependent")
.Select(o => (int)o)
.ToList()
我用这个XML。它存储在 c:\data 文件夹
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<Id>1</Id>
<EmployeeId>1</EmployeeId>
<EmployeeName>Bob</EmployeeName>
<ChiefID></ChiefID>
<ChiefName></ChiefName>
<Dependent>
<idDependent>2</idDependent>
<idDependent>3</idDependent>
<idDependent>4</idDependent>
<idDependent>5</idDependent>
</Dependent>
</Employee>
</Employees>
使用下面给定的代码
XDocument doc = XDocument.Load("c:\data\Employees.xml");
var employees = doc.Descendants("Employee");
foreach (var employee in employees)
{
Employee emp = new Employee();
emp.ID = Convert.ToInt32(employee.Element("Id").Value);
emp.EmployeeID = Convert.ToInt32(employee.Element("EmployeeId").Value);
emp.EmployeeName = employee.Element("EmployeeName").Value;
int ChiefIdValue;
int.TryParse(employee.Element("ChiefID").Value, out ChiefIdValue);
emp.ChiefID = ChiefIdValue;
emp.ChiefName = employee.Element("ChiefName").Value;
emp.ListOfEmployees = employee.Element("Dependent").Descendants("idDependent").Select(x => Convert.ToInt32(x.Value)).ToList<int>();
}
我有一个 class:
class Employee
{
public int? ID { get; set; }
public int? EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int? ChiefID { get; set; }
public string ChiefName { get; set; }
public List<int> ListOfEmployees { get; set; }
}
而xml是:
<?xml version="1.0" encoding="UTF8"?>
<Employee>
<Id>1</Id>
<EmloyeeId>1</EmployeeId>
<EmIoyeeName>Bob</EmIoyeeName>
<ChiefID></ChiefID>
<ChiefName></ChiefName>
<Dependent>
<idDependent>2</idDependent>
<idDependent>3</idDependent>
<idDependent>4</idDependent>
<idDependent>5</idDependent>
</Dependent>
</Employee>
</Employees>
我的 C# 代码是:
XDocument xDoc=LoadXML();
IEnumerable<Employee> sourceEmployee=xDoc.Descendants("Employee").Select(d=>new Employee {
ID=(int?)d.Element("Id"),
EmployeeID=(int?)d.Element("EmloyeeId"),
ListOfEmloyees=(List<int>)d.Element("Dependent").Elements("idDependent")//cannot populate ListOfEmloyees
});
但是,我无法将数据读取到 ListOfEmployes 属性。是否可以读取 "Dependent" 节点的所有后代值?
将 XElement
转换为 int
然后调用 .ToList()
:
ListOfEmloyees = d.Elements("Dependent")
.Elements("idDependent")
.Select(o => (int)o)
.ToList()
我用这个XML。它存储在 c:\data 文件夹
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<Id>1</Id>
<EmployeeId>1</EmployeeId>
<EmployeeName>Bob</EmployeeName>
<ChiefID></ChiefID>
<ChiefName></ChiefName>
<Dependent>
<idDependent>2</idDependent>
<idDependent>3</idDependent>
<idDependent>4</idDependent>
<idDependent>5</idDependent>
</Dependent>
</Employee>
</Employees>
使用下面给定的代码
XDocument doc = XDocument.Load("c:\data\Employees.xml");
var employees = doc.Descendants("Employee");
foreach (var employee in employees)
{
Employee emp = new Employee();
emp.ID = Convert.ToInt32(employee.Element("Id").Value);
emp.EmployeeID = Convert.ToInt32(employee.Element("EmployeeId").Value);
emp.EmployeeName = employee.Element("EmployeeName").Value;
int ChiefIdValue;
int.TryParse(employee.Element("ChiefID").Value, out ChiefIdValue);
emp.ChiefID = ChiefIdValue;
emp.ChiefName = employee.Element("ChiefName").Value;
emp.ListOfEmployees = employee.Element("Dependent").Descendants("idDependent").Select(x => Convert.ToInt32(x.Value)).ToList<int>();
}