c# - 读取 XML 没有返回任何结果

标签 c# xml linq-to-xml

我有以下 xml,我需要读取以下值:

<po-response xmlns="http://test.com<" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="rest/oms/export/v3/purchase-order.xsd">
  <!--Generated on Sellpo host [spapp402p.prod.ch4.s.com]-->
  <purchase-order>
    <customer-order-confirmation-number>123456</customer-order-confirmation-number>
    <customer-email>test@test.com</customer-email>
    <po-number>00001</po-number>
    <po-date>2012-02-12</po-date>
    <po-time>06:58:40</po-time>
    <po-number-with-date>12100000000</po-number-with-date>
    <unit>123</unit>
    <site>Test</site>
    <channel>VD</channel>
    <location-id>1234</location-id>
    <expected-ship-date>2012-02-13</expected-ship-date>
    <shipping-detail>
      <ship-to-name>JON DOE</ship-to-name>
      <address>123 SOMETHING STREET</address>
      <city>NEW NEW</city>
      <state>PS</state>
      <zipcode>BG121</zipcode>
      <phone>012030401</phone>
      <shipping-method>Ground</shipping-method>
    </shipping-detail>
  </purchase-order>
</po-response>

我正尝试按如下方式从 shipping-detail 元素中提取信息,但没有返回任何信息?

                xmlDoc = XDocument.Parse(sr.ReadToEnd());

                var details = from detail in xmlDoc.Descendants("shipping-detail")
                                select new
                                {
                                    Name = detail.Element("ship-to-name").Value,
                                    Address = detail.Element("Address").Value,
                                    City = detail.Element("city").Value,
                                };


                foreach (var detail in details)
                {
                    Console.WriteLine("Ship to Name: " + detail.Name);
                    Console.WriteLine("Ship to Name: " + detail.Address);
                    Console.WriteLine("Ship to Name: " + detail.City);
                }

最佳答案

当前您的 XML 无效 - 假设您的命名空间声明是这样的xmlns="http://test.com",您可以使用命名空间获取您的节点:

xmlDoc = XDocument.Parse(sr.ReadToEnd());
XNamespace ns = "http://test.com";
var details = from detail in xmlDoc.Descendants(ns + "shipping-detail")
                select new
                {
                    Name = detail.Element(ns + "ship-to-name").Value,
                    Address = detail.Element(ns + "address").Value,
                    City = detail.Element(ns + "city").Value,
                };

另请记住,节点名称区分大小写,因此它是 "address" 而不是 "Address"

关于c# - 读取 XML 没有返回任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9269194/

相关文章:

javascript - 如何使用 javascript 调用 UserControl 中的函数(代码隐藏)

java - 根据列表项的位置替换 fragment

javascript - 来自 PHP 的最新 XML 文件响应未加载到 Javascript 中

jquery - 使用 jQuery 读取 RSS 提要

c# - NetworkStream 获取 System.IO.IOException : Unable to write data to the transport connection

c# - 应用程序在 Visual Studio 的单元测试中以 x86 运行,但在独立时以 x64 运行

c# - 如何使用 Linq to SQL 添加到列表?

c# - 帮助 Linq to Xml 查询

c# - 如何使用 LINQ-to-XML 从 XML 中排除 NULL block ?

c# - XElement.Elements().Select() 原因 尝试显式指定类型参数