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

发布于 2024-10-24 20:13:06 字数 1838 浏览 4 评论 0原文

我想从以下 XML 字符串(来自 Yahoo 地理编码 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>

我已经将此 XML 成功加载到 XElement 实例中,但我似乎无法找到加载纬度节点的方法(例如)转换为字符串变量。如果没有节点或节点为空,那么我想获取 Null 或 Nullstring。如果有多个实例(不会有但以防万一)则返回第一个实例。

我以为这很容易,但我无法让它发挥作用。我尝试过的所有 Linq 查询都返回 null。

当我在做的时候,如果你能用足够的细节解释它,以便我也能得到错误节点。我只是提到它,因为它处于不同的层次。

谢谢。

赛斯

I want to return the latitude node (for example) from the following XML string (from 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. If there is no node or the node is empty then I would like to get a Null or 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.

While I am at it if you could explain it with enough detail so that I can also get the Error node. I only mention it because it is at a different level.

Thanks.

Seth

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-10-31 20:13:06

要获取纬度值:

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

您可以通过以下方式获取 Error 元素:

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

我使用 resultXML 作为对已解析 XML 的引用。

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:

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

I'm using resultXML as the reference to the parsed XML.

草莓味的萝莉 2024-10-31 20:13:06

确保您使用的是 System.Xml.XPath 命名空间,并尝试:

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

el 应包含 XElement 类或 null< /code> 如果未找到节点。

请参阅 MSDN 文档了解 XPath 1.0 了解有关如何使用它的详细信息。

Make sure you're using the System.Xml.XPath namespace, and try:

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.

See the MSDN docs for XPath 1.0 for more info on how to use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文