我有以下需要读取的值的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);
}发布于 2012-02-14 06:52:33
目前您的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"。
发布于 2012-02-14 06:53:20
您的查询代码片段:
xmlDoc.Descendants("shipping-detail") 将查看Root节点。您没有任何名为"shipping-detail“的根节点。
试一试
xmlDoc.Root.Descendants("shipping-detail")https://stackoverflow.com/questions/9269194
复制相似问题