我想使用 Linq 从 XML 元素返回唯一/单个字符串值
我想从以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获取纬度值:
您可以通过以下方式获取 Error 元素:
我使用
resultXML
作为对已解析 XML 的引用。To get latitude's value:
And you could get the Error element with the following:
I'm using
resultXML
as the reference to the parsed XML.确保您使用的是
System.Xml.XPath
命名空间,并尝试:el
应包含XElement
类或null< /code> 如果未找到节点。
请参阅 MSDN 文档了解 XPath 1.0 了解有关如何使用它的详细信息。
Make sure you're using the
System.Xml.XPath
namespace, and try:el
should contain anXElement
class ornull
if the node wasn't found.See the MSDN docs for XPath 1.0 for more info on how to use it.