读取响应XML文件

本文关键字:文件 XML 响应 读取 | 更新日期: 2023-09-27 17:50:12

我想用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>

读取响应XML文件

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

使用nsa的当前值

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

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

你能试一下吗?

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

我现在可以访问地址元素里面的元素了