读取 XML 没有返回任何结果

发布于 2025-01-05 08:41:53 字数 2326 浏览 1 评论 0原文

我有以下 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>[email protected]</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);
                }

I have the following xml I need to read the values of:

<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>[email protected]</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>

I'm trying to extract the information from the shipping-detail element as follows but nothing is being brought back?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑕疵 2025-01-12 08:41:53

目前您的 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"

Currently your XML is invalid - assuming your namespace declaration is like this xmlns="http://test.com", you can get your nodes using the namespace:

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,
                };

Also keep in mind that node names are case-sensitive, so it's "address" and not "Address".

岁月打碎记忆 2025-01-12 08:41:53

您的查询片段:

xmlDoc.Descendants("shipping-detail") 

将查看根节点。您没有任何名为“shipping-detail”的根节点。

尝试

xmlDoc.Root.Descendants("shipping-detail")

Your query snippet:

xmlDoc.Descendants("shipping-detail") 

is going to look at the Root node. You don't have any Root nodes named "shipping-detail".

Try

xmlDoc.Root.Descendants("shipping-detail")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文