C# 使用XDocument.Load加载XML的子体

C# 使用XDocument.Load加载XML的子体,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我正在尝试使用XDocument.Load访问一些纬度和经度数据。下面是示例XML文档 <?xml version="1.0" encoding="utf-8"?> <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/

我正在尝试使用XDocument.Load访问一些纬度和经度数据。下面是示例XML文档

<?xml version="1.0" encoding="utf-8"?>
<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 © 2016 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>31a206847f9341d28689e0e7185e163d|DB40051719|7.7.0.0|DB4SCH010061262</TraceId>
  <ResourceSets>
    <ResourceSet>
      <EstimatedTotal>1</EstimatedTotal>
      <Resources>
        <Location>
          <Name>SW1A 1AA, London, London, United Kingdom</Name>
          <Point>
            <Latitude>51.501018524169922</Latitude>
            <Longitude>-0.14159967005252838</Longitude>
          </Point>
          <BoundingBox>
            <SouthLatitude>51.497155806599245</SouthLatitude>
            <WestLongitude>-0.14987251765942367</WestLongitude>
            <NorthLatitude>51.5048812417406</NorthLatitude>
            <EastLongitude>-0.1333268224456331</EastLongitude>
          </BoundingBox>
          <EntityType>Postcode1</EntityType>
          <Address>
            <AdminDistrict>England</AdminDistrict>
            <AdminDistrict2>London</AdminDistrict2>
            <CountryRegion>United Kingdom</CountryRegion>
            <FormattedAddress>SW1A 1AA, London, London, United Kingdom</FormattedAddress>
            <Locality>London</Locality>
            <PostalCode>SW1A 1AA</PostalCode>
          </Address>
          <Confidence>High</Confidence>
          <MatchCode>Good</MatchCode>
          <GeocodePoint>
            <Latitude>51.501018524169922</Latitude>
            <Longitude>-0.14159967005252838</Longitude>
            <CalculationMethod>Rooftop</CalculationMethod>
            <UsageType>Display</UsageType>
          </GeocodePoint>
        </Location>
      </Resources>
    </ResourceSet>
  </ResourceSets>
</Response>

但这将返回一个空字符串。如何正确导航文档?

如果要获取所有
GeocodePoint
节点,首先不需要调用multiples level
substands
方法。您只能执行以下操作:

XNamespace ns =  "http://schemas.microsoft.com/search/local/ws/rest/v1";
string latitude = XDocument.Load(@"test.xml")
                  .Descendants(ns+"GeocodePoint")
                  .Select(e=> (string)e.Element(ns+"Latitude"))
                  .FirstOrDefault();
通过调用linqtoxml将检索XML中的所有
GeocodePoints

如果要获取lat和long值,则可以投影到匿名类型或自定义类(DTO),如下所示:

XNamespace ns =  "http://schemas.microsoft.com/search/local/ws/rest/v1";

var coord= XDocument.Load(@"xml.xml")
          .Descendants(ns+"GeocodePoint").Select(e => new { Lat = (string)e.Element(ns+"Latitude"), Lng = (string)e.Element(ns+"Longitude") })
          .FirstOrDefault();
关于你的问题
您的问题是调用
Attribute
方法来获取
Latitude
值,但正如您在xml结构
GeocodePoint
节点中看到的那样,它不是一个属性,而是一个嵌套元素。这就是您需要使用
Element
方法的方式。第二个问题是您需要考虑名称空间,如我上面所示。

如果要获取所有
GeocodePoint
节点,首先不需要调用multiples-level
subjects
方法。您只能执行以下操作:

XNamespace ns =  "http://schemas.microsoft.com/search/local/ws/rest/v1";
string latitude = XDocument.Load(@"test.xml")
                  .Descendants(ns+"GeocodePoint")
                  .Select(e=> (string)e.Element(ns+"Latitude"))
                  .FirstOrDefault();
通过调用linqtoxml将检索XML中的所有
GeocodePoints

如果要获取lat和long值,则可以投影到匿名类型或自定义类(DTO),如下所示:

XNamespace ns =  "http://schemas.microsoft.com/search/local/ws/rest/v1";

var coord= XDocument.Load(@"xml.xml")
          .Descendants(ns+"GeocodePoint").Select(e => new { Lat = (string)e.Element(ns+"Latitude"), Lng = (string)e.Element(ns+"Longitude") })
          .FirstOrDefault();
关于你的问题
您的问题是调用
Attribute
方法来获取
Latitude
值,但正如您在xml结构
GeocodePoint
节点中看到的那样,它不是一个属性,而是一个嵌套元素。这就是您需要使用
Element
方法的方式。第二个问题是您需要考虑名称空间,正如我上面所示。

您没有使用名称空间。您的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">
或者通过元素的
LocalName
进行不带名称空间的搜索

string value = doc.Root
                  .Descendants
                  .Where(element => element.Name.LocalName.Equals("Latitude"))
                  .FirstOrDefault()
                  .Value;

如果您使用的是
degenerats
方法,那么您可以直接搜索所需的元素。

您没有使用名称空间。您的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">
或者通过元素的
LocalName
进行不带名称空间的搜索

string value = doc.Root
                  .Descendants
                  .Where(element => element.Name.LocalName.Equals("Latitude"))
                  .FirstOrDefault()
                  .Value;

如果您使用
degenerats
方法,则可以直接搜索所需的元素。

感谢您的时间和帮助。不幸的是,这仍然会向我返回两个空字符串。是的,我注意到现在您正在使用
Attribute
方法,您应该使用
Element
而不是。不幸的是,当我调用这些方法时,仍然是空字符串。是的,经过测试,我发现您需要在查询中包含名称空间。好样的家伙:)谢谢您的时间和帮助。不幸的是,这仍然会向我返回两个空字符串。是的,我注意到现在您正在使用
属性
方法,您应该使用
元素
而不是。不幸的是,当我调用这些方法时,仍然是空字符串。是的,在我测试之后,我发现您需要在查询中包含名称空间。很好的复制人:)