Windows7移动Silverlight:Linq到XML问题
我有一个XML文档:Windows7移动Silverlight:Linq到XML问题,silverlight,windows-mobile,linq-to-xml,Silverlight,Windows Mobile,Linq To Xml,我有一个XML文档: <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>blahblah</title> <subtitle>blahblah.</subtitle> <link href="blahblah"/> <link href="blahblah"/&g
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>blahblah</title>
<subtitle>blahblah.</subtitle>
<link href="blahblah"/>
<link href="blahblah"/>
<updated>blahblah</updated>
<author>
<name>blahblah</name>
</author>
<id>blahblah</id>
<entry>
<title>blahblah</title>
<content type="html"><![CDATA[“some text.”]]></content>
</entry>
</feed>
xml文档加载正常(通过调试可以看出),但它一直抛出“未找到顺序项”错误。如何查找“内容”节点信息?只要XML有名称空间(由
xmlns
属性指示),就必须通过引用元素的名称空间来引用元素。这是通过将名称空间连接到元素名来实现的
另外,您使用了xmlTreeVal
,但随后引用了xmlTreeVerse
——这可能不是问题所在,但您所展示的代码中存在不一致之处
试试这个:
var ns = xmlTreeVerse.Root.GetDefaultNamespace();
var returnVal = from item in xmlTreeVerse.Descendants(ns + "feed").Elements(ns + "entry")
select new
{
Verse = item.Element(ns + "content").Value
};
非常感谢。您必须使用它的具体原因是什么?我曾读到您应该使用它来避免命名冲突,但我不知道您必须使用它。@如果在XML中指定了它,则必须使用它。您可以使用
Where
子句作为解决方法,如中所示,但它不如直接引用命名空间有效。如果XML没有名称空间,那么可以直接按名称查询节点。
var ns = xmlTreeVerse.Root.GetDefaultNamespace();
var returnVal = from item in xmlTreeVerse.Descendants(ns + "feed").Elements(ns + "entry")
select new
{
Verse = item.Element(ns + "content").Value
};