Windows 7 Mobile Silverlight : Linq to XML issue
I have a XML Document:
<?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>
I want to get the entry nodes content information, here's my code:
var xmlTreeVal = XDocument.Load("myxml.xml");
var returnVal = from item in xm开发者_如何转开发lTreeVerse.Descendants("feed").Elements("entry")
select new VerseModel
{
Verse = item.Element("content").Value
};
The xml document is loading fine (as i could tell via debugging), but it keeps throwing a 'No sequential items found' error. How do I find the "content" nodes info?
Whenever your XML has a namespace (indicated by the xmlns
attribute) you must reference the elements by also referencing their namespace. This is done by concatenating the namespace to the element name.
Also, you used xmlTreeVal
but then reference xmlTreeVerse
- it's probably not the problem but it's an inconsistency in the code you presented.
Try this:
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
};
精彩评论