简体   繁体   English

无法从收到的XML文件中获取列表

[英]Cant get list from received XML file

This is xml that I receive: 这是我收到的xml:

<?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:ZipCode>data3</mdpr:ZipCode>
      <mdpr:City>data4</mdpr:City>
    </mdpr:contact>
    <mdpr:contact ID="{234567}" classID="Customer">
      <mdpr:Name>data5</mdpr:Name>
      <mdpr:TransportCode>data6</mdpr:TransportCode>
      <mdpr:ZipCode>data7</mdpr:ZipCode>
      <mdpr:City>data8</mdpr:City>
    </mdpr:contact>
</mdpr:contactList>
...

Here is how I try to get all contacts: 以下是我尝试获取所有联系人的方式:

public class Contact
    {
        public string Name { get; set; }
        public string TransportCode { get; set; }
    }
...
XDocument xdoc = XDocument.Load(doc.CreateNavigator().ReadSubtree());

            List<Contact> contacts = (from xml in xdoc.Elements("contactList").Elements("contact")
                                      select new Contact
                              {
                                  Name = xml.Element("Name").Value,
                                  TransportCode = xml.Element("TransportCode").Value
                              }).ToList();

But I get nothing. 但我一无所获。 What am I doing wrong here? 我在这做错了什么?

try to specify namespace 尝试指定命名空间

string nmsp = "http://www.w3.org/2001/XMLSchema-instance/"

from xml in xdoc.Elements(nmsp+"contactList").Elements(nmsp + "contact")

You have mdpr namespace declared in your xml: 您在xml中声明了mdpr名称空间:

xmlns:mdpr="http://..."

but you are providing only local name of elements in query. 但是您只在查询中提供元素的本地名称。 Eg you provide contactList name, but full name of element is mdpr:contactList . 例如,您提供了contactList名称,但元素的全名是mdpr:contactList That's why nothing is found. 这就是找不到任何东西的原因。

You should define XNamespace for your namespace and use it to create full names of elements: 您应该为命名空间定义XNamespace ,并使用它来创建元素的全名:

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")
               };

Also contactList is not the root of document. contactList也不是文档的根。 You should search it under Root . 你应该在Root下搜索它。

XDocument xdoc = XDocument.Load(doc.CreateNavigator().ReadSubtree());
var ns = xdoc.Root.Name.Namespace;
List<Contact> contacts = (from xml in xdoc.Root.Elements(ns +"contactList").Elements(ns +"contact")
                          select new Contact
                  {
                      Name = xml.Element(ns +"Name").Value,
                      TransportCode = xml.Element(ns +"TransportCode").Value
                  }).ToList();  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM