我有一个创建XMLDocument的REST web服务,我对如何使用XMLNode访问FormattedPrice中的内部文本感到有点困惑。我可以提供学位,但这将给我所有的内在文本。
<Offers>
<Offer>
<OfferListing>
<Price>
<Amount>1067</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$10.67</FormattedPrice>
</Price>
</OfferListing>
</Offer>
</Offers>发布于 2011-08-06 01:15:56
如果您正在使用Xml,一个快速的walk-through of XPath将非常有帮助。
这应该可以满足您的即时需求:
XmlNode n = doc.DocumentElement.SelectSingleNode("Offer/OfferListing/Price/FormattedPrice");这将得到第一个优惠的格式化价格(并假设您的优惠节点是根节点)。在XPath中还存在一些不那么脆弱的机制,这就是教程可以帮助您的地方。
发布于 2011-08-06 01:16:26
你最好使用XPath。
XmlDocument doc = ...;
XmlNode fPrice;
XmlElement root = doc.DocumentElement;
fPrice= root.SelectSingleNode("/Price/FormattedPrice");
return fPrice.InnerText;这里有一个很好的例子:http://www.codeproject.com/KB/cpp/myXPath.aspx
发布于 2011-08-06 01:14:49
使用XElement对其进行解析:
string tmp = @"
<Offers>
<Offer>
<Price>
<Amount>1067</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$10.67</FormattedPrice>
</Price>
</Offer>
</Offers>";
XElement xml = XElement.Parse(tmp);
string formatedPrice = (string)xml.XPathSelectElement("/Offer/Price/FormattedPrice");https://stackoverflow.com/questions/6960040
复制相似问题