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?

5 Answers 5

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(); 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

1 Comment

You can also do away with new StringReader and just use d.LoadXml(xml)
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.

1 Comment

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

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.