在 C# 中使用 LINQ-To-XML 解析具有多个列表和类对象的 XML 数据
Posted
技术标签:
【中文标题】在 C# 中使用 LINQ-To-XML 解析具有多个列表和类对象的 XML 数据【英文标题】:Parse XML data with Multiple List & Class objects using LINQ-To-XML in C# 【发布时间】:2018-09-08 08:57:09 【问题描述】:假设我要解析以下 XML 文件:
<EmployeeDetails>
<Employee> //List of Employees
<Id>11</Id>
<name>a</name>
<Dependents> //List of Dependents of a single employee
<Dependent>
<name>a1</name>
<age>50</age>
</Dependent>
<Dependent>
<name>a2</name>
<age>52</age>
</Dependent>
</Dependents>
<Department> //Unique per Emp
<DeptId>1</DeptId>
<DeptName>D1</DeptName>
</Department>
</Employee>
<Employee>
-----
--------
</Employee>
</EmployeeDetails>
以下是上述文件的类结构:
public class Employee
public int id get; set;
public string name get; set;
public List<Dependents> Dependents get; set;
public Department Department get; set;
public class Dependents
public string name get; set;
public int age get; set;
public class Department
public int DeptId get; set;
public string DeptName get; set;
现在,我想解析上面的 XML 结构,我可以为 Employee
的 id
和 name
解析,但我无法进一步解析。
让我告诉你我到目前为止做了什么:
public static void ParseXml()
string xmldoc = //let's assume I've data in this string
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(xmldoc);
var query = from d in xdoc.Root.Descendants("Employee")
select d;
List<Employee> lsEmp = new List<Employee>();
foreach (var q in query)
Employee obj = new Employee();
obj.Id = Convert.ToInt32(q.Element("Id").Value);
obj.name = q.Element("name").Value;
obj.Department = new Department();
obj.Dependents = new List<Dependents>();
// how to get data further?
lsEmp.Add(obj);
所以我需要帮助才能从 Dependents
和 Department
对象的这些列表中解析 XML 数据。
【问题讨论】:
如果你写了这段代码,如果我理解你的问题,你已经知道如何钻取节点:q.Element("Id")
& q.Element("name")
,那么问题是什么?
您考虑过使用 XmlSerializer 吗?如果类定义正确,它将一口气读完。
@AndrewTruckle 我知道使用 XMLSerializer 很容易,但我不会使用 LINQ-TO-XML 来实现这一点
【参考方案1】:
按照您自己的结构,以下是继续解析您需要的数据的方法。
// how to get data further?
var allDependents = q.Elements("Dependents").Elements("Dependent");
foreach (var b in allDependents)
Dependents d = new Dependents
age = Convert.ToInt32(b.Element("age").Value),
name = b.Element("name").Value
;
obj.Dependents.Add(d);
obj.Department.DeptId = Convert.ToInt32(q.Element("Department").Element("DeptId").Value);
obj.Department.DeptName = q.Element("Department").Element("DeptName").Value;
请注意,我使用.Elements("")
来获取Dependents
下的所有子节点
【讨论】:
【参考方案2】:这是只使用 linq 而没有 for 循环的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
class Program
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
string xml = File.ReadAllText(FILENAME);
ParseXml(xml);
public static void ParseXml(string xml)
XDocument xdoc = XDocument.Parse(xml);
List<Employee> employees = xdoc.Descendants("Employee").Select(x => new Employee ()
id = (int)x.Element("Id"),
name = (string)x.Element("Name"),
Department = x.Elements("Department").Select(y => new Department() DeptId = (int)y.Element("DeptId"), DeptName = (string)y.Element("DeptName")).FirstOrDefault(),
Dependents = x.Descendants("Dependent").Select(y => new Dependents() age = (int)y.Element("age"), name = (string)y.Element("name")).ToList()
).ToList();
public class Employee
public int id get; set;
public string name get; set;
public List<Dependents> Dependents get; set;
public Department Department get; set;
public class Dependents
public string name get; set;
public int age get; set;
public class Department
public int DeptId get; set;
public string DeptName get; set;
【讨论】:
感谢您提供不同的方法。 +1。【参考方案3】:也许这可以帮助你:
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(xmldoc);
var query = from d in xdoc.Root.Descendants("Employee")
select d;
List<Employee> lsEmp = new List<Employee>();
foreach (var q in query)
Employee obj = new Employee();
obj.Id = Convert.ToInt32(q.Element("Id").Value);
obj.name = q.Element("name").Value;
obj.Department = new Department()
DeptName = q.Element("Department").Element("name").Value,
DeptId =
Convert.ToInt32(q.Element("Department").Element("age").Value)
;
obj.Dependents = new List<Dependents>();
foreach (var e in q.Element("Dependents").Elements("Dependent"))
var dependent = new Dependents()
name = e.Element("name").Value,
age = Convert.ToInt32(e.Element("age").Value)
;
obj.Dependents.Add(dependent);
lsEmp.Add(obj);
【讨论】:
以上是关于在 C# 中使用 LINQ-To-XML 解析具有多个列表和类对象的 XML 数据的主要内容,如果未能解决你的问题,请参考以下文章