【发布时间】:2018-04-23 09:32:19
【问题描述】:
我对如何从 XML 读取数据有问题。 XML 看起来像这样:
<PosXML version="7.2.0">
<ReadCardResponse>
<ReturnCode>1</ReturnCode>
<Card>
<Pan>222300******5062</Pan>
<Expires>****</Expires>
<CardName>MASTERCARD</CardName>
<CardSource>2</CardSource>
</Card>
</ReadCardResponse>
</PosXML>
我已经从流中加载了 XML:
XDocument doc;
using (Stream responseStream = httpResponse.GetResponseStream())
{
doc= XDocument.Load(responseStream);
}
试过了,还是不行:
XElement returnCode = doc.XPathSelectElement("ReturnCode")
【问题讨论】:
-
XPath
ReturnCode无效。对于您的 XML,XPath 应该类似于(未经测试的)ReadCardResponse\ReturnCode。您可以查看 XPath 文档以了解更复杂的 XPath 查询 -
阅读XPath Expressions。 XPath 表达式可用于选择任何节点或属性。然后,您可以将
SelectNodes或SelectSingleNode与 XPath 表达式一起使用到要读取的节点。
标签: c# xml linq-to-xml