今天第二篇文章在第一篇文章被回答之后我又遇到了另一个问题!
基本上我有以下代码。我知道除了一件事情它是正确的,“:”字符是不被允许的并引发异常,是否有人知道解决这个问题的最佳方法?
location = resultElements.Element("channel").Element("yweather:location").Attribute("city").Value
由于
答案 0 :(得分:1)
我假设您具有与your previous question中描述的完全相同的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:
或<geo:
开头的元素时,您必须使用该命名空间。因此,请使用yweather + "location"
代替yweather:location
:
Dim location = resultElements.Element("channel") _
.Element(yweather + "location") _
.Attribute("city") _
.Value