简体   繁体   English

在C#WP8中解析xml

[英]Parse xml in C# WP8

I am relative new with windows phone so I don't know how to parse a document retrieved from the internet. 我是Windows手机的新手,所以我不知道如何解析从互联网上检索到的文件。

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. 我想设置在我的POI类的实例中检索的每个poi,其中包含xml中的所有字段。

By this I print the xml: 通过这个我打印xml:

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

My class POI has this fields: 我的班级POI有以下字段:

Strings: name, description, thumbnal, url; 字符串:名称,描述,缩略图,网址; Doubles: lat, lon 双打:lat,lon

And my xml seems like this: 我的xml看起来像这样:

        <?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. 我有很多地标(POI),都有相同的字段。 I want to have an array with all POI retrieved. 我想要一个包含所有POI检索的数组。

I tried with something like this but it says 0 using count: 我试过这样的东西,但它说0使用计数:

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. 如果我能得到地标的ID,那将是有用的。 I could add another var to my POI class. 我可以在我的POI类中添加另一个var。

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: 编辑:我尝试了一个用户在他的答案中说,并通过给出异常崩溃:System.Xml.Linq.ni.dll中发生System.ArgumentNullException ...我添加修改后的代码:

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

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. 没有返回任何结果,因为Placemark元素不是数据中的根元素。 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: 如果您想要在文档中获取所有Placemark元素,无论它们位于何处,那么您可以执行以下操作:

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. 您必须获取信息和Point元素才能查询它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM