tags:

views:

550

answers:

4
+2  Q: 

simple xml parsing

what is the simplest way to parse the lat and long out of the following xml fragment. There is no namespace etc.

It is in a string variable. not a stream.

<poi>
      <city>stockholm</city>
      <country>sweden</country>
      <gpoint>
        <lat>51.1</lat>
        <lng>67.98</lng>
      </gpoint>
</poi>

everything I have read so far is waaaaay too complex for what should be a simple task e.g. http://geekswithblogs.net/kobush/archive/2006/04/20/75717.aspx

I've been looking at the above link

Surely there is a simpler way to do this in .net?

+4  A: 
using System.IO;
using System.Xml;
using System.Xml.XPath;

. . .

    string xml = @"<poi>      
                     <city>stockholm</city>  
                     <country>sweden</countr>
                        <gpoint>        
                            <lat>51.1</lat>        
                            <lng>67.98</lng>    
                        </gpoint>
                   </poi>";

    XmlReaderSettings set = new XmlReaderSettings();
    set.ConformanceLevel = ConformanceLevel.Fragment;

    XPathDocument doc = 
        new XPathDocument(XmlReader.Create(new StringReader(xml), set));

    XPathNavigator nav = doc.CreateNavigator();


    Console.WriteLine(nav.SelectSingleNode("/poi/gpoint/lat"));
    Console.WriteLine(nav.SelectSingleNode("/poi/gpoint/lng"));

You could of course use xpath SelectSingleNode to select the <gpoint> element into a variable.

Mitch Wheat
thanks a lot - that is much more straight forward
Christo Fur
+5  A: 

Using Linq for XML:

   XDocument doc= XDocument.Parse("<poi><city>stockholm</city><country>sweden</country><gpoint><lat>51.1</lat><lng>67.98</lng></gpoint></poi>");

    var points=doc.Descendants("gpoint");

    foreach (XElement current in points)
    {
        Console.WriteLine(current.Element("lat").Value);
        Console.WriteLine(current.Element("lng").Value);
    }

    Console.ReadKey();
Ash
+1. Nice 3.5 version...
Mitch Wheat
+1  A: 

If you can use LINQ for XML you can read those two values straight forward like this:

var doc = XDocument.Parse("...");
var point = doc.Element("poi").Element("gpoint");
Console.WriteLine("Lat: {0}, Lng: {1}",
    point.Element("lat").Value,
    point.Element("lng").Value);

If you need them as double you have to convert them:

double lat = Convert.ToDouble(point.Element("lat").Value,
                 CultureInfo.InvariantCulture);

Don't forget to specify a culture that uses a dot for fractions. Otherwise this would yield 511.0 as latitude.

gix
That's an ok approach, but I prefer Descendents because it is less dependent on the document structure and might be less likely to fail if there were changes.
Ash
+1  A: 

Even simpler than Mitch Wheat's answer, since the fragment in question is a well-formed XML document:

using System.Xml;
using System.IO;

...

string xml = @"<poi>
                  <city>stockholm</city>
                  <country>sweden</country>
                  <gpoint>
                    <lat>51.1</lat>
                    <lng>67.98</lng>
                  </gpoint>
              </poi>";

XmlDocument d = new XmlDocument();
d.Load(new StringReader(xml));
Console.WriteLine(d.SelectSingleNode("/poi/gpoint/lat").InnerText);
Console.WriteLine(d.SelectSingleNode("/poi/gpoint/lng").InnerText);
Robert Rossney