1

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[&ldquo;some text.&rdquo;]]></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 xmlTreeVerse.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?

contactmatt
  • 18,116
  • 40
  • 128
  • 186

1 Answers1

3

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
                };
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
  • Thank You! Is there a specific reason that you MUST use it? I had read that you should use it to avoid naming conflicts, but I didn't know you HAD to use it. – contactmatt Nov 14 '10 at 04:40
  • @contactmatt if it's specified in your XML you must use it. You can use the `Where` clause as a workaround as demonstrated in [this question](http://stackoverflow.com/q/1145659), but it's not as efficient as referencing the namespace directly. If your XML didn't have a namespace then you can query the nodes by name directly. – Ahmad Mageed Nov 14 '10 at 04:53