简体   繁体   English

如何使用linq在XML中获取位置信息?

[英]How to grab location information in the XML using linq?

My xml looks like: 我的xml看起来像:

<kml xmlns="http://earth.google.com/kml/2.0">
  <Response> 
    <name>90210</name> 
    <Status> 
      <code>200</code> 
      <request>geocode</request> 
    </Status> 
    <Placemark id="p1"> 
      <address>Beverly Hills, CA 90210, USA</address> 
      <AddressDetails Accuracy="5" 
        xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
        <Country>
          <CountryNameCode>US</CountryNameCode>
          <CountryName>USA</CountryName>
          <AdministrativeArea>
            <AdministrativeAreaName>CA</AdministrativeAreaName>
            <SubAdministrativeArea>
              <SubAdministrativeAreaName>Los 
                Angeles</SubAdministrativeAreaName>
              <Locality>
                <LocalityName>Beverly Hills</LocalityName>
                <PostalCode>
                  <PostalCodeNumber>90210</PostalCodeNumber>
                </PostalCode>
              </Locality>
            </SubAdministrativeArea>
          </AdministrativeArea></Country>
          </AddressDetails> 
          <ExtendedData> 
            <LatLonBox north="34.1377559" south="34.0642330" 
              east="-118.3896720" west="-118.4467160" /> 
          </ExtendedData> 
          <Point>
            <coordinates>-118.4104684,34.1030032,0</coordinates>
          </Point> 
    </Placemark> 
  </Response>
</kml> 

I need the information in extended data ie values for north/south, east/west. 我需要扩展数据中的信息,即北/南,东/西的值。

You can also drill down though all of the nodes. 您还可以深入所有节点。 But this is probably the simplest way to get the LatLonBox element. 但这可能是获取LatLonBox元素的最简单方法。

var xml = XElement.Parse(xmlString);
var ns = "{http://earth.google.com/kml/2.0}";
var extendedData = xml.Descendants(ns + "LatLonBox").First();

var locationBox = new
{
    North = float.Parse(extendedData.Attribute("north").Value),
    South = float.Parse(extendedData.Attribute("south").Value),
    East = float.Parse(extendedData.Attribute("east").Value),
    West = float.Parse(extendedData.Attribute("west").Value),
};

... to drill down the elements you can do this ... ...深入研究您可以执行的操作...

var extendedData = xml.Element(ns + "Response")
                      .Element(ns + "Placemark")
                      .Element(ns + "ExtendedData")
                      .Element(ns + "LatLonBox");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM