【发布时间】:2018-07-28 08:56:08
【问题描述】:
我想从以下 XML 示例节点中获取 UPC 值,但当前的 doc.SelectNodes 无法获取该值。我正在使用 XmlDocument 来处理我的 XML。你能修复我的代码以获取 UPC 值吗?我在这里做错了什么?
C#代码:
string responseStr = new StreamReader(responseStream).ReadToEnd();
responseStream.Flush();
responseStream.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(responseStr);
if (doc.GetElementsByTagName("Ack").Item(0).InnerText != "Failure")
{
string UPC = doc.SelectNodes("Item").Item(0).SelectNodes("UPC").Item(0).InnerText;
}
XML 示例:
<?xml version="1.0" encoding="UTF-8"?>
<GetItemResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2018-07-28T08:18:10.048Z</Timestamp>
<Ack>Success</Ack>
<Version>1069</Version>
<Build>E1069_CORE_API_18748854_R1</Build>
<Item>
<ProductListingDetails>
<ISBN>Not Applicable</ISBN>
<UPC>853365007036</UPC>
<EAN>0853365007036</EAN>
<BrandMPN>
<Brand>UpCart</Brand>
<MPN>MPCB-1DX</MPN>
</BrandMPN>
<IncludeeBayProductDetails>true</IncludeeBayProductDetails>
</ProductListingDetails>
</Item>
</GetItemResponse>
【问题讨论】:
-
您可以选择任何其他值吗?我的意思是来自另一个节点?
-
不,它说我选择的东西不可用。
-
您需要在调用
SelectNodes()时指定正确的命名空间,即urn:ebay:apis:eBLBaseComponents。参见例如XmlDocument.SelectSingleNode and xmlNamespace issue 和 Can't get XmlDocument.SelectNodes to retrieve any of my nodes? 和 XPath with XmlDocument not finding nodes. -
以上都是真的。还有下面的答案。但是为什么你使用
SelectNodes而不是GetElementsByTagName就像你使用"Ack"那样?
标签: c# xml xmldocument