【问题标题】:Reading response XML file读取响应 XML 文件
【发布时间】:2015-05-21 12:22:51
【问题描述】:

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

代码:

 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>

【问题讨论】:

    标签: c# xml linq web-services


    【解决方案1】:

    您使用了错误的命名空间。文档的根命名空间(由 XML 文档的无别名 xmlns 属性表示)是 http://schemas.microsoft.com/search/local/ws/rest/v1

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

    【讨论】:

      【解决方案2】:
      1. 创建一个与 XML 结构匹配的类/模型和 将 XML 反序列化为对象或集合。
      2. 如果需要,可以使用 Linq 处理收集。

      看看https://msdn.microsoft.com/en-us/library/tz8csy73(v=vs.110).aspx

      【讨论】:

        【解决方案3】:

        你可以试试这个:

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

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

        【讨论】:

          猜你喜欢
          • 2017-10-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-13
          • 2016-04-25
          • 2016-07-31
          相关资源
          最近更新 更多