通过 linq 到 xml 的复杂类型映射
Posted
技术标签:
【中文标题】通过 linq 到 xml 的复杂类型映射【英文标题】:Complex type mapping via linq to xml 【发布时间】:2013-08-14 21:20:02 【问题描述】:我在 XML 文件中有一个联系人列表。
每个联系人都有一些属性和mdpr:connection
。
连接是单独的对象。
我阅读了这个列表并将所有联系人都添加到一个具有标准属性的列表中,但是如何将此连接映射到对象。
<?xml version="1.0" encoding="UTF-8"?>
<mdpr:Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mdpr="http://...">
<mdpr:contactList>
<mdpr:contact ID="123456" classID="Customer">
<mdpr:Name>data1</mdpr:Name>
<mdpr:TransportCode>data2</mdpr:TransportCode>
<mdpr:connection connectionIndex="0" fromID="12345" toID="123456">
<mdpr:status>1-5</mdpr:status>
<mdpr:startDate>2012-03-13T10:23:00Z</mdpr:startDate>
<mdpr:endDate>2013-03-13T13:44:00Z</mdpr:endDate>
</mdpr:connection>
</mdpr:contact>
</mdpr:contactList>
...
Classes:
public class Contact
public string Name get; set;
public string TransportCode get; set;
public Connection Connection get; set;
public TransportPlan()
this.Connection = new Connection();
public class Connection
public string status get; set;
public string startDate get; set;
public string endDate get; set;
读取数据的代码:
XNamespace mdpr = "http://...";
var contacts = from c in xdoc.Root.Element(mdpr + "contactList")
.Elements(mdpr + "contact")
select new Contact
TransportCode = (string)c.Element(mdpr + "TransportCode"),
Name = (string)c.Element(mdpr + "Name")
;
那么问题是如何阅读mdpr:connection
?
【问题讨论】:
【参考方案1】:您可以通过添加另一个“.Element”来直接访问这些元素。我添加了一个变量以提高可读性。
var contacts = from c in xdoc.Element(mdpr + "Data")
.Element(mdpr + "contactList")
.Elements(mdpr + "contact")
let contact = c
let connection = contact.Element(mdpr + "connection")
select new Contact
TransportCode = (string)contact.Element(mdpr + "TransportCode"),
Name = (string)contact.Element(mdpr + "Name"),
Connection = new Connection
status = (string)connection.Element(mdpr + "status"),
startDate = (string) connection.Element(mdpr + "startDate"),
endDate = (string)connection.Element(mdpr + "endDate"),
,
;
如果你想允许多个连接(为了让场景更复杂)
public class Contact
public string Name get; set;
public string TransportCode get; set;
public List<Connection> Connections get; set;
解析多个连接的代码
var contacts = from c in xdoc.Element(mdpr + "Data")
.Element(mdpr + "contactList")
.Elements(mdpr + "contact")
let contact = c
let connections = contact.Elements(mdpr + "connection")
select new Contact
TransportCode = (string)contact.Element(mdpr + "TransportCode"),
Name = (string)contact.Element(mdpr + "Name"),
Connections = connections.Select( connection =>
new Connection
status = (string)connection.Element(mdpr + "status"),
startDate = (string) connection.Element(mdpr + "startDate"),
endDate = (string)connection.Element(mdpr + "endDate"),
).ToList(),
;
【讨论】:
以上是关于通过 linq 到 xml 的复杂类型映射的主要内容,如果未能解决你的问题,请参考以下文章