通过linq到xml的复杂类型映射

时间:2013-08-13 09:57:16

标签: c# xml xml-parsing linq-to-xml xmldocument

我有XML文件中的联系人列表 每个联系人都有一些属性,其中包含mdpr:connection 连接是单独的对象 我阅读了这个列表,并将所有联系人都列入了标准proeprties的列表,但是如何将此Connection映射到object。

<?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 个答案:

答案 0 :(得分:2)

您可以通过添加另一个“.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(),
               };