加载xml文档时出现异常

时间:2013-05-18 16:20:13

标签: c# xml windows-phone-7

我正在开发一个Windows Phone应用程序。在访问bing映射时,加载xml文档时出现异常“XmlException - Name不能以'。'开头。字符“在行XDocument result = XDocument.Load(r);请帮忙。

这是我的代码

private void locationtoaddress()
    {
        String url = "http://dev.virtualearth.net/REST/v1/Locations/" + latitude + "," + longitude + "?o=xml&key=" + bingKey;
        WebClient wc = new WebClient();
        wc.DownloadStringAsync(new Uri(url));
        wc.DownloadStringCompleted += wc_DownloadStringCompleted;
    }

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        string s = e.Result;
        XmlReader r = XmlReader.Create(new MemoryStream(UnicodeEncoding.Unicode.GetBytes(s)));
        XDocument result = XDocument.Load(r);
        var abc= result.Root.GetDefaultNamespace();
        var address1 = from query in result.Descendants(abc + "Location")
                       select new Location
                       {
                           address = (string)query.Element(abc + "Name")
                       };
        Location loc = new Location();
        MessageBox.Show(loc.address);
                }

这是我获得的价值

<?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 © 2013 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>a017c5068e364703bc8bdbb58f508d96|SINM001009|02.00.139.700|SINMSNVM001117, 32.151</TraceId><ResourceSets><ResourceSet><EstimatedTotal>5</EstimatedTotal><Resources><Location><Name>NH-49, Sampatti Puram, Madurai, India</Name><Point><Latitude>9.9334483</Latitude><Longitude>78.0918096</Longitude></Point><BoundingBox><SouthLatitude>9.9334153</SouthLatitude><WestLongitude>78.0918519</WestLongitude><NorthLatitude>9.9339407</NorthLatitude><EastLongitude>78.0909383</EastLongitude></BoundingBox><EntityType>Address</EntityType><Address><AddressLine>NH-49</AddressLine><AdminDistrict>Tamil Nadu</AdminDistrict><AdminDistrict2>Madurai</AdminDistrict2><CountryRegion>India</CountryRegion><FormattedAddress>NH-49, Sampatti Puram, Madurai, India</FormattedAddress><Locality>Madurai</Locality></Address><Confidence>Medium</Confidence><MatchCode>Good</MatchCode><GeocodePoint><Latitude>9.9334483</Latitude><Longitude>78.0918096</Longitude><CalculationMethod>Interpolation</CalculationMethod><UsageType>Display</UsageType><UsageType>Route</UsageType></GeocodePoint></Location><Location><Name>NH-49, Sampatti Puram, Madurai, India</Name><Point><Latitude>9.9334153</Latitude><Longitude>78.0918519</Longitude></Point><BoundingBox><SouthLatitude>9.9332373</SouthLatitude><WestLongitude>78.0920988</WestLongitude><NorthLatitude>9.9334153</NorthLatitude><EastLongitude>78.0918519</EastLongitude></BoundingBox><EntityType>Address</EntityType><Address><AddressLine>NH-49</AddressLine><AdminDistrict>Tamil Nadu</AdminDistrict><AdminDistrict2>Madurai</AdminDistrict2><CountryRegion>India</CountryRegion><FormattedAddress>NH-49, Sampatti Puram, Madurai, India</FormattedAddress><Locality>Madurai</Locality></Address><Confidence>Medium</Confidence><MatchCode>Good</MatchCode><GeocodePoint><Latitude>9.9334153</Latitude><Longitude>78.0918519</Longitude><CalculationMethod>Interpolation</CalculationMethod><UsageType>Display</UsageType><UsageType>Route</UsageType></GeocodePoint></Location><Location><Name>Bethaniapuram, Madurai, India</Name><Point><Latitude>9.9334153</Latitude><Longitude>78.0918519</Longitude></Point><BoundingBox><SouthLatitude>9.9334153</SouthLatitude><WestLongitude>78.0918519</WestLongitude><NorthLatitude>9.9345254</NorthLatitude><EastLongitude>78.0910617</EastLongitude></BoundingBox><EntityType>Address</EntityType><Address><AddressLine>Bethaniapuram</AddressLine><AdminDistrict>Tamil Nadu</AdminDistrict><AdminDistrict2>Madurai</AdminDistrict2><CountryRegion>India</CountryRegion><FormattedAddress>Bethaniapuram, Madurai, India</FormattedAddress><Locality>Madurai</Locality></Address><Confidence>Medium</Confidence><MatchCode>Good</MatchCode><GeocodePoint><Latitude>9.9334153</Latitude><Longitude>78.0918519</Longitude><CalculationMethod>Interpolation</CalculationMethod><UsageType>Display</UsageType><UsageType>Route</UsageType></GeocodePoint></Location><Location><Name>Sampatti Puram, Madurai, India</Name><Point><Latitude>9.9328814</Latitude><Longitude>78.0917037</Longitude></Point><BoundingBox><SouthLatitude>9.9328051</SouthLatitude><WestLongitude>78.0919259</WestLongitude><NorthLatitude>9.9328814</NorthLatitude><EastLongitude>78.0917037</EastLongitude></BoundingBox><EntityType>Address</EntityType><Address><AddressLine>Sampatti Puram</AddressLine><AdminDistrict>Tamil Nadu</AdminDistrict><AdminDistrict2>Madurai</AdminDistrict2><CountryRegion>India</CountryRegion><FormattedAddress>Sampatti Puram, Madurai, India</FormattedAddress><Locality>Madurai</Locality></Address><Confidence>Medium</Confidence><MatchCode>Good</MatchCode><GeocodePoint><Latitude>9.9328814</Latitude><Longitude>78.0917037</Longitude><CalculationMethod>Interpolation</CalculationMethod><UsageType>Display</UsageType><UsageType>Route</UsageType></GeocodePoint></Location><Location><Name>Bharathiyar Street, Sampatti Puram, Madurai, India</Name><Point><Latitude>9.9330578</Latitude><Longitude>78.092027</Longitude></Point><BoundingBox><SouthLatitude>9.9328051</SouthLatitude><WestLongitude>78.0919259</WestLongitude><NorthLatitude>9.9332373</NorthLatitude><EastLongitude>78.0920988</EastLongitude></BoundingBox><EntityType>Address</EntityType><Address><AddressLine>Bharathiyar Street</AddressLine><AdminDistrict>Tamil Nadu</AdminDistrict><AdminDistrict2>Madurai</AdminDistrict2><CountryRegion>India</CountryRegion><FormattedAddress>Bharathiyar Street, Sampatti Puram, Madurai, India</FormattedAddress><Locality>Madurai</Locality></Address><Confidence>Medium</Confidence><MatchCode>Good</MatchCode><GeocodePoint><Latitude>9.9330578</Latitude><Longitude>78.092027</Longitude><CalculationMethod>Interpolation</CalculationMethod><UsageType>Display</UsageType><UsageType>Route</UsageType></GeocodePoint></Location></Resources></ResourceSet></ResourceSets></Response>

1 个答案:

答案 0 :(得分:4)

您的问题是您将字符串转换为UTF-16,即使XDocument期望UTF-8。

使用正确的编码:

string s = e.Result;
XmlReader r = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(s)));
XDocument result = XDocument.Load(r);

或者更简单地说,直接使用XDocument.Parse

string s = e.Result;
XDocument result = XDocument.Parse(s);