今天第二个帖子在第一个被回答之后我遇到了另一个问题!
基本上我有下面的代码。我知道除了一件事之外它是正确的,“:”字符是不允许的并引发异常,有人知道解决这个问题的最佳方法吗?
location = resultElements.Element("channel").Element("yweather:location").Attribute("city").Value
谢谢
今天第二个帖子在第一个被回答之后我遇到了另一个问题!
基本上我有下面的代码。我知道除了一件事之外它是正确的,“:”字符是不允许的并引发异常,有人知道解决这个问题的最佳方法吗?
location = resultElements.Element("channel").Element("yweather:location").Attribute("city").Value
谢谢
我假设您的 XML 输入与上一个问题中描述的完全相同。
在第一个元素中声明了两个命名空间:
<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
version="2.0">
您必须先为它们声明XNamespace
实例:
Dim yweather = XNamespace.Get("http://xml.weather.yahoo.com/ns/rss/1.0")
当您查询以<yweather:
or开头的元素时,您必须使用该命名空间<geo:
。所以使用yweather + "location"
而不是yweather:location
:
Dim location = resultElements.Element("channel") _
.Element(yweather + "location") _
.Attribute("city") _
.Value