sso*_*ola 4 c# xml windows-phone-7
我试图用C#解析复杂的XML,我正在使用Linq来做.基本上,我正在向服务器发出请求,我得到XML,这是代码:
XElement xdoc = XElement.Parse(e.Result);
this.newsList.ItemsSource =
from item in xdoc.Descendants("item")
select new ArticlesItem
{
//Image = item.Element("image").Element("url").Value,
Title = item.Element("title").Value,
Description = this.Strip(item.Element("description").Value).Substring(0, 200).ToString()
}
Run Code Online (Sandbox Code Playgroud)
这是XML结构:
<item>
<test:link_id>1282570</test:link_id>
<test:user>SLAYERTANIC</test:user>
<title>aaa</title>
<description>aaa</description>
</item>
Run Code Online (Sandbox Code Playgroud)
我如何访问属性test:link_id例如?
谢谢!
目前您的XML无效,因为test未声明命名空间,您可以这样声明:
<item xmlns:test="http://foo.bar">
<test:link_id>1282570</test:link_id>
<test:user>SLAYERTANIC</test:user>
<title>aaa</title>
<description>aaa</description>
</item>
Run Code Online (Sandbox Code Playgroud)
有了这个,您可以使用XNamespace正确的命名空间来限定所需的XML元素:
XElement xdoc = XElement.Parse(e.Result);
XNamespace test = "http://foo.bar";
this.newsList.ItemsSource = from item in xdoc.Descendants("item")
select new ArticlesItem
{
LinkID = item.Element(test + "link_id").Value,
Title = item.Element("title").Value,
Description = this.Strip(item.Element("description").Value).Substring(0, 200).ToString()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
961 次 |
| 最近记录: |