2011-07-18 13 views
1

私はWP7をコーディングしています。文句を言わない、それを読んで、_itemsに置きc#子孫の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ループ:私は、下記のXMLを読み込むには、以下のコードを期待しています。

<?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> 
+0

手動ではなくXMLシリアル化を検討しましたか? XMLは、シンプルなクラスで記述可能(それは単語ですか?)であるはずです。 –

答えて

1

各要素名に名前空間がないようです。これを試してみてください:

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); 
} 
+0

ありがとうございました!それだ。 –

+0

助けてくれてうれしいです。私が最初に遭遇したとき、私は頭が傷ついていたことは間違いありませんでした。 :) –

0

子孫を使用する特別な理由はありますか?

あなただけ行うことができます:

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を持っていないが、それはそれに近いものでなければなりません。 もちろん、ノードが次のノードを取得する前にヌルであるかどうかを確認するのがよいでしょう。

+0

申し訳ありませんが、XmlDocumentクラスを持たないWP7については言及しませんでしたので、これは不可能です。ありがとう。 –

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

これは私がやる方法です。