简体   繁体   English

我想使用Linq从XML元素返回唯一/单个字符串值

[英]I want to return the only/single string value from an XML Element using Linq

I want to return the latitude node (for example) from the following XML string (from Yahoo geocoding API.) 我想从以下XML字符串(例如,来自Yahoo geocoding API)返回纬度节点。

<ResultSet version="1.0">
  <Error>0</Error>
  <ErrorMessage>No error</ErrorMessage>
  <Locale>us_US</Locale>
  <Quality>60</Quality>
  <Found>1</Found>
  <Result>
    <quality>87</quality>
    <latitude>37.68746446</latitude>
    <longitude>-79.6469878</longitude>
    <offsetlat>30.895931</offsetlat>
    <offsetlon>-80.281192</offsetlon>
    <radius>500</radius>
    <name></name>
    <line1>123 Main Street</line1>
    <line2>Greenville, SC  29687</line2>
    <line3></line3>
    <line4>United States</line4>
    <house>123</house>
    <street>Main Street</street>
    <xstreet></xstreet>
    <unittype></unittype>
    <unit></unit>
    <postal>29687</postal>
    <neighborhood></neighborhood>
    <city>Greenville</city>
    <county>Greenville County</county>
    <state>South Carolina</state>
    <country>United States</country>
    <countrycode>US</countrycode>
    <statecode>SC</statecode>
    <countycode></countycode>
    <uzip>29687</uzip>
    <hash>asdfsdfas</hash>
    <woeid>127757446454</woeid>
    <woetype>11</woetype>
  </Result>
</ResultSet>

I already have this XML successfully loaded into an XElement instance but I cannot seem to be able to find the way to load the latitude node (for example) into a string variable. 我已经将该XML成功加载到XElement实例中,但是我似乎无法找到将纬度节点(例如)加载到字符串变量中的方法。 If there is no node or the node is empty then I would like to get a Null or Nullstring. 如果没有节点或该节点为空,那么我想获取一个Null或Nullstring。 If there is more than one (there won't be but just in case) then return the first instance. 如果有多个(不会,只是为了以防万一),则返回第一个实例。

I thought this would be easy but I can't get it to work. 我以为这很容易,但是我无法正常工作。 All of the Linq queries I have tried are returning null. 我尝试过的所有Linq查询都返回null。

While I am at it if you could explain it with enough detail so that I can also get the Error node. 当我在这里时,如果您可以详细解释它,以便我也可以获取Error节点。 I only mention it because it is at a different level. 我只提到它是因为它处于不同的水平。

Thanks. 谢谢。

Seth 赛斯

To get latitude's value: 要获取纬度值:

var latitudeElement = resultXML.Descendants("latitude").FirstOrDefault();
string latitude = latitudeElement == null ? String.Empty : latitudeElement.Value;

And you could get the Error element with the following: 您可以使用以下内容获取Error元素:

var errorElement = resultXML.Descendants("Error").First();

I'm using resultXML as the reference to the parsed XML. 我正在使用resultXML作为对已解析XML的引用。

Make sure you're using the System.Xml.XPath namespace, and try: 确保您正在使用System.Xml.XPath命名空间,然后尝试:

var doc = XDocument.Parse(<your xml here>);
var el = doc.XPathSelectElement("ResultSet/Result/latitude");

el should contain an XElement class or null if the node wasn't found. el应该包含一个XElement类;如果找不到该节点,则为null

See the MSDN docs for XPath 1.0 for more info on how to use it. 有关如何使用XPath 1.0的更多信息,请参见MSDN文档

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

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