1

I'm learning to use ReST Web services and I need to find out how to get a specific value from the xml string that is returned. How can I simply get 1 value from an xml String? All I want is one value. Is there some way to convert this string into something with an indexer?

I'm using Yahoo Geocoding service. Results:

<ResultSet version="1.0">
<Error>0</Error>
<ErrorMessage>No error</ErrorMessage>
<Locale>us_US</Locale>
<Quality>87</Quality>
<Found>1</Found>
−
<Result>
<quality>85</quality>
<latitude>86.457310</latitude>
<longitude>-73.262245</longitude>
<offsetlat>46.457311</offsetlat>
<offsetlon>-73.262071</offsetlon>
<radius>500</radius>
<name/>
<line1>1234 N Main St</line1>
<line2>Anytown, New York  12345</line2>
<line3/>
<line4>United States</line4>
<house>1234</house>
<street>N Main St</street>
<xstreet/>
<unittype/>
<unit/>
<postal>12345</postal>
<neighborhood/>
<city>New York</city>
<county>Albany County</county>
<state>New York</state>
<country>United States</country>
<countrycode>US</countrycode>
<statecode>NY</statecode>
<countycode/>
<uzip>12345</uzip>
<hash>E692D20CBDF86A2E</hash>
<woeid>12783988</woeid>
<woetype>11</woetype>
</Result>
</ResultSet>
4
  • Maybe simply XmlDocument.SelectNodes? Commented Feb 10, 2011 at 17:41
  • Ok, but how do I get from having a string to an xml document? Commented Feb 10, 2011 at 17:43
  • there are many ways to do this, to show you a sample xml would be good Commented Feb 10, 2011 at 17:46
  • @BrokenGlass- I understand there are many ways to do it, but I can't seem to find an example for a simple way to do it. Commented Feb 10, 2011 at 19:32

3 Answers 3

3

You could use Linq to XML

XDocument xmlfile= XDocument.Load("PATH TO XML DOC");
var test = from xml in xmlfile.Descendants("item_name")
           select new { Title = (string)xml.Element("title").Value };

That's one way.

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

3 Comments

No file; data comes by way of a string
Where is the string coming from?
I'm not sure if it matters (a string is a string, isn't it?) but I've added sample xml response to my question.
2

Use XPath to address the node you are interested in:

http://msdn.microsoft.com/en-us/library/d271ytdx(v=VS.90).aspx

To transform XML string into an XML document

XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);

Here is a good introduction to XPath: http://www.codeproject.com/KB/cpp/myXPath.aspx

3 Comments

Can I do this using a string in memory instead of an xml file?
Perfect! Could you also show an example of how to look at a specific element (node?).
Wow, I can't believe they didn't include a constructor for strings OR files! Microsoft....
1

See my question on Easiest way to read XML with attributes. I found that using xsd.exe to generate a xsd which allows you managed access to the XML was the simplest way to access the XML data. LINQ2XML was also pretty easy to use.

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.