2

I have a REST web service that creates a XMLDocument I am a bit confused on how access the inner text in FormattedPrice using XMLNode. I can grad offers but that will give me all the inner text.

<Offers>
    <Offer>
       <OfferListing>
          <Price>
            <Amount>1067</Amount>
            <CurrencyCode>USD</CurrencyCode>
            <FormattedPrice>$10.67</FormattedPrice>
          </Price>
       </OfferListing>
    </Offer>
</Offers>
2
  • 1
    Does it have to be an XmlDocument (and not an XDocument) ? Commented Aug 5, 2011 at 17:18
  • @Henk I am using an third party library that uses XmlDocument. Commented Aug 5, 2011 at 17:29

6 Answers 6

5

A quick walk-through of XPath will help immensely if you're using the Xml DOM.

This should meet your immediate need:

XmlNode n = doc.DocumentElement.SelectSingleNode("Offer/OfferListing/Price/FormattedPrice");

That will get you the first Offer's Formatted price (and assumes that your Offers node is the root). Other mechanisms exist in XPath that are a bit less brittle and that's where a tutorial would help you.

Sign up to request clarification or add additional context in comments.

1 Comment

should probably give the complete path, just so a maintainer knows what it going on: "//Offers/Offer[0]/Price/FormattedPrice"
4

You are probably be best off using XPath.

XmlDocument doc = ...;

XmlNode fPrice;
XmlElement root = doc.DocumentElement;
fPrice= root.SelectSingleNode("/Price/FormattedPrice");

return fPrice.InnerText;

Here's a good example: http://www.codeproject.com/KB/cpp/myXPath.aspx

Comments

0

Use XElement to parse it:

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

2 Comments

The OP is using XMLDocument/XMLNode, not XDocument/XElement :)
@Rob: You could convert though.
0

This should grab the $ amount:

var price = doc.SelectSingleNode(@"//Offer/Price/FormattedPrice");
string priceText = price.InnerText;

Comments

0

Load what you want into a XmlNodeList and then pull one explicitly or loop through them...

XmlNodeList pricesList = xmlDoc.GetElementsByTagName("FormattedPrice");
string firstPrice = pricesList[0].InnerText;

Comments

0

First off, your XML is invalid....you are missing the starting OfferListing tag.

Here is yet another option to grab the node text.

var xmlString = "<Offers><Offer><OfferListing><Price><Amount>1067</Amount<CurrencyCode>USD</CurrencyCode><FormattedPrice>$10.67</FormattedPrice></Price></OfferListing></Offer></Offers>";

var xDoc = new XmlDocument();
xDoc.LoadXml(xmlString);
var formattedPrice = xDoc.GetElementsByTagName("FormattedPrice")[0].InnerText;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.