读取响应XML文件

时间:2015-05-21 12:22:52

标签: c# xml linq web-services

我想用C#LINQ阅读下面的XMLfile 我在C#中尝试过这段代码。但是无法获取地址元素数据。

CODE:

 XNamespace ns = "http://www.w3.org/2001/XMLSchema";
 XNamespace nsa = "http://www.w3.org/2001/XMLSchema-instance";

 var Address = from r in XDocumentdata.Descendants(ns + "Address")
               select new
                     {
                       Locality = r.Element(nsa + "Locality").Value,
                       CountryRegion = r.Element(nsa + "CountryRegion").Value
                     };

 foreach (var r in Address)
 {
     string CountryRegion = r.CountryRegion;
     string Locality = r.Locality;
 }

XML:

<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
  <Copyright>Copyright © 2015 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>1cf1896a29234bc583b75487b57e343f|HK20271655|02.00.163.1200|HK2SCH010280821, i-d219511f.ap-southeast-1b</TraceId>
      <ResourceSets>
        <ResourceSet>
          <EstimatedTotal>5</EstimatedTotal>
          <Resources>
            <Location>
              <Name>Panjagutta, Hyderabad 500082, India</Name>
              <Point>
                <Latitude>17.4176132</Latitude>
                <Longitude>78.449595</Longitude>
              </Point>
              <BoundingBox>
                <SouthLatitude>17.41759</SouthLatitude>
                <WestLongitude>78.44907</WestLongitude>
                <NorthLatitude>17.41764</NorthLatitude>
                <EastLongitude>78.4502</EastLongitude>
              </BoundingBox>
              <EntityType>Address</EntityType>
              <Address>
                <AddressLine>Panjagutta</AddressLine>
                <AdminDistrict>TS</AdminDistrict>
                <AdminDistrict2>Hyderabad</AdminDistrict2>
                <CountryRegion>India</CountryRegion>
                <FormattedAddress>Panjagutta, Hyderabad 500082, India</FormattedAddress>
                <Locality>Hyderabad</Locality>
                <PostalCode>500082</PostalCode>
              </Address>
              <Confidence>Medium</Confidence>
              <MatchCode>Good</MatchCode>
              <GeocodePoint>
                <Latitude>17.4176132</Latitude>
                <Longitude>78.449595</Longitude>
                <CalculationMethod>Interpolation</CalculationMethod>
                <UsageType>Display</UsageType>
                <UsageType>Route</UsageType>
              </GeocodePoint>
            </Location>

          </Resources>
        </ResourceSet>
      </ResourceSets>
    </Response>

3 个答案:

答案 0 :(得分:1)

您使用的是错误的命名空间。文档的根命名空间,由XML文档的未标记xmlns属性表示为http://schemas.microsoft.com/search/local/ws/rest/v1

使用它而不是nsa的当前值。

答案 1 :(得分:1)

  1. 创建一个与XML和XML结构相匹配的类/模型 将XML反序列化为对象或集合。
  2. 如果需要,请使用Linq处理集合。
  3. 查看https://msdn.microsoft.com/en-us/library/tz8csy73(v=vs.110).aspx

答案 2 :(得分:0)

你可以尝试一下:

var xmlDoc = new XmlDocument();
xmlDoc.Load("your xml path");
var address = xmlDoc.GetElementsByTagName("Address");

我可以访问Address元素中的元素。