c#XML来自子代

本文关键字:c#XML | 更新日期: 2023-09-27 18:02:46

我正在为WP7编码。我希望下面的代码能阅读下面的XML:

_xml = XElement.Parse(e.Result);
                results.Items.Clear();
                foreach (XElement value in _xml
                    .Descendants("ResourceSets").Descendants("ResourceSet")
                    .Descendants("Resources").Descendants("Location"))
                {
                    Results _item = new Results();
                    _item.Place = value.Element("Name").Value;
                    _item.Lat = value.Element("Point").Element("Latitude").Value;
                    _item.Long = value.Element("Point").Element("Longitude").Value;
                    results.Items.Add(_item);
                }

但是foreach循环不会读取它并将其放置在_items中。

<?xml version="1.0" encoding="utf-8" ?> 
 <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
  <Copyright>Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright> 
  <BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri> 
  <StatusCode>200</StatusCode> 
  <StatusDescription>OK</StatusDescription> 
  <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode> 
  <TraceId>703e7f1427dd425185ded546ba8a0d2c|LTSM001154|02.00.126.3000|LTSMSNVM002008, LTSMSNVM001854, LTSMSNVM001853</TraceId> 
 <ResourceSets>
 <ResourceSet>
  <EstimatedTotal>4</EstimatedTotal> 
 <Resources>
 <Location>
  <Name>Ashford, Kent, United Kingdom</Name> 
 <Point>
  <Latitude>51.146636679768562</Latitude> 
  <Longitude>0.87603025138378143</Longitude> 
  </Point>
 <BoundingBox>
  <SouthLatitude>51.076602190732956</SouthLatitude> 
  <WestLongitude>0.72853825986385345</WestLongitude> 
  <NorthLatitude>51.21656522154808</NorthLatitude> 
  <EastLongitude>1.0235222429037094</EastLongitude> 
  </BoundingBox>
  <EntityType>PopulatedPlace</EntityType> 
 <Address>
  <AdminDistrict>England</AdminDistrict> 
  <AdminDistrict2>Kent</AdminDistrict2> 
  <CountryRegion>United Kingdom</CountryRegion> 
  <FormattedAddress>Ashford, Kent, United Kingdom</FormattedAddress> 
  <Locality>Ashford</Locality> 
  </Address>
  <Confidence>High</Confidence> 
  </Location>
  </Resources>
  </ResourceSet>
  </ResourceSets>
  </Response>

c#XML来自子代

看起来每个元素名称上都缺少名称空间。试试这个:

XNamespace xns = "http://schemas.microsoft.com/search/local/ws/rest/v1";
_xml = XElement.Parse(e.Result);
results.Items.Clear();
foreach (XElement value in _xml
    .Descendants(xns + "ResourceSets").Descendants(xns + "ResourceSet")
    .Descendants(xns + "Resources").Descendants(xns + "Location"))
{
    Results _item = new Results();
    _item.Place = value.Element(xns + "Name").Value;
    _item.Lat = value.Element(xns + "Point").Element(xns + "Latitude").Value;
    _item.Long = value.Element(xns + "Point").Element(xns + "Longitude").Value;
    results.Items.Add(_item);
}

使用Descendants有什么特别的原因吗?

你可以做:

XmlDocument doc = new XmlDocument();
doc.Load(YourXMLPath);
XmlNode locationNode = doc["ResourceSets"]["ResourceSet"]["Resources"]["Location"];
foreach(XmlElement value in locationNode.Children)
{
    Results _item = new Results();
                    _item.Place = value.Element("Name").Value;
                    _item.Lat = value.Element("Point").Element("Latitude").Value;
                    _item.Long = value.Element("Point").Element("Longitude").Value;
                    results.Items.Add(_item);
}

我现在没有VS,但应该已经接近了。当然,一个好的行为是在获取下一个节点之前检查节点是否为空。

public ObservableCollection<Results> result = new ObservableCollection<Results>();
XDocument xmldoc = XDocument.Parse(e.Result.ToString());
var data = from c in xmldoc.Descendants("ResourceSets").Descendants("ResourceSet").Descendants("Resources").Descendants("Location")
    select new Results{
    Place = c.Element("Name").Value;
    Lat = c.Element("Point").Element("Latitude").Value;
    Long = c.Element("Point").Element("Longitude").Value;
    };
    foreach (Results obj in data) 
    {
        result.Add(obj);
    }

还没有尝试过,但我就是这样做的。