0

I am relative new with windows phone so I don't know how to parse a document retrieved from the internet.

Now I am able to get it and print in it by console. I want to set each poi retrieved in an instance of my POI class which has all fields from the xml.

By this I print the xml:

XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
System.Diagnostics.Debug.WriteLine(xdoc.FirstNode.ToString());

My class POI has this fields:

Strings: name, description, thumbnal, url; Doubles: lat, lon

And my xml seems like this:

        <?xml version="1.0" encoding="utf-8"?>
        <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:default="http://www.opengis.net/kml/2.2" 
         default:ar="http://www.openarml.org/arml/1.0"
         default:wikitude="http://www.openarml.org/wikitude/1.0">
         <Document>
                  <ar:provider xmlns:ar="http://www.openarml.org/arml/1.0"
                       id="myid.com">
                  <ar:name>Name of the owner</ar:name>
                  <ar:description><![CDATA[Description of the xml]]></ar:description>
                  <wikitude:providerUrl xmlns:wikitude="http://www.openarml.org/wikitude/1.0">www.webpage.es</wikitude:providerUrl>
                  <wikitude:tags xmlns:wikitude="http://www.openarml.org/wikitude/1.0">tag1,tag2,tag3,tag4</wikitude:tags>
                  <wikitude:logo xmlns:wikitude="http://www.openarml.org/wikitude/1.0">http://mylogourl.png</wikitude:logo>
                  <wikitude:icon xmlns:wikitude="http://www.openarml.org/wikitude/1.0">www.myiconurl.png</wikitude:icon></ar:provider>

        <default:Placemark id="id53">
              <ar:provider xmlns:ar="http://www.openarml.org/arml/1.0">myid.com</ar:provider>
              <default:name>PLACE</default:name>
              <default:description><![CDATA[Description of the place.]]></default:description>
              <wikitude:info xmlns:wikitude="http://www.openarml.org/wikitude/1.0">
                <wikitude:thumbnail>http://urltothethumbnail/</wikitude:thumbnail>
                <wikitude:url>http://urltotheplace.com</wikitude:url>
              </wikitude:info>
              <default:Point>
                <default:coordinates>-3.0000000000,43.0000000000,0</default:coordinates>
              </default:Point>
            </default:Placemark>


             </default:Document>
        </default:kml>

I have lots of placemarks (POI), all with the same fields. I want to have an array with all POI retrieved.

I tried with something like this but it says 0 using count:

var pois = xdoc.Root
            .Elements("Placemark")
            .Select(poi => new POI(
                       (String)stop.Attribute("name"),
                       (String)stop.Attribute("description"),
                       (String)stop.Attribute("thumbnail"),
                       (String)stop.Attribute("url"),
                       (Double)stop.Attribute("lat"),
                       (Double)stop.Attribute("lon")))
            .ToList();

If I can get the id of the placemark, it would be useful. I could add another var to my POI class.

EDIT: I tried what one user said in his answer and it crash by giving an exception: System.ArgumentNullException occurred in System.Xml.Linq.ni.dll... I add the modified code:

XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
XNamespace ns = "http://www.opengis.net/kml/2.2";
            var placemarks = xdoc.Root.Descendants(ns + "Placemark");


            var pois = placemarks
            .Select(poi => new POI(
                       (String)poi.Attribute("name"),
                       (String)poi.Attribute("description"),
                       (String)poi.Attribute("thumbnail"),
                       (String)poi.Attribute("url"),
                       (Double)poi.Attribute("lat"),
                       (Double)poi.Attribute("lon")))
            .ToList();


            System.Diagnostics.Debug.WriteLine(pois.Count);

1 Answer 1

2

The following code in your original post:

xdoc.Root.Elements("Placemark")

is not returning any results because the Placemark element is not a root element in your data. Also, you've not specified the namespace. If you want to get all Placemark elements in the document, regardless of where they might be located, then you could do something like this:

XNamespace ns = "http://www.opengis.net/kml/2.2";
var placemarks = xdoc.Root.Descendants(ns + "Placemark");

Your original code will also fail to retrieve the other information (name, description, thumbnail, etc...) because none of that data is actually stored in attributes. Those are all contained in elements, not attributes, and you'll also need to specify the correct namespaces when querying them (see how the namespace was specified in my example above). Finally, note that the latitude, longitude, thumbnail, and url data are contained in sub-elements. You'll have to grab the info and Point elements in order to query those.

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

3 Comments

Please, see my updated question. I have an exception using your answer. Maybe I misunderstood you
I amended my answer to address some of the other issues in your original code.
I already tried and it stores all my pois. Now I will try to add the sub-elements to my POI (I deleted to test it) but I will try on my own keeping your answer in mind. Thanks

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.