1

我在 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

4

1 回答 1

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(),
               };
于 2013-08-14T06:37:18.667 回答