9

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. Link

I've been looking at the above link

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ChrisCa
  • 10,876
  • 22
  • 81
  • 118

5 Answers5

19

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
  • 60,973
  • 31
  • 151
  • 169
7
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
  • 295,962
  • 43
  • 465
  • 541
6

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
  • 94,622
  • 24
  • 146
  • 218
3

Use XElement.Parse(string text) method to do this.

Example:

string xmlString = @"<poi>
              <city>stockholm</city>
              <country>sweden</country>
              <gpoint>
                <lat>51.1</lat>
                <lng>67.98</lng>
              </gpoint>
          </poi>";
        try
        {
            XElement x = XElement.Parse(xmlString);
            var latLng = x.Element("gpoint");

            Console.WriteLine(latLng.Element("lat").Value);
            Console.WriteLine(latLng.Element("lng").Value);
        }
        catch
        {
        }

Hope this helps!

Mohamed Alikhan
  • 1,315
  • 11
  • 14
3

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
  • 5,737
  • 5
  • 34
  • 40
  • 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 Feb 16 '09 at 04:21