views:

60

answers:

1

Book List XSLT Programmers Reference Michael Kay

from the given Xml document,I want to iterate all <blist:books> elements.

(i.e) How to i handle the namespace ?

i tried

XNamespace blist = XNamespace.Get("http://www.wrox.com/books/xml");
XElement element = XElement.Load("Books.xml");
IEnumerable<XElement> titleElement =
            from el in element.Elements(blist + "books") select el;

but the enumeration (titleElement) does not return any result.

A: 

You have two different problems here.

  1. You're calling the Elements method, which returns the direct child elements of the element you call it on. Since the <html> element has no direct <blist:books> child elements, you aren't getting any results.
  2. XML is case sensitive. You need to write books, not Books.

Also, there's no point in writing from el in whatever select el. Unless you put addition logic (such as a where or orderby clause, or a non-trivial select), you should just write whatever.

Therefore, you need to replace your LINQ query with the following:

IEnumerable<XElement> titleElement = element.Descendants(blist + "books");
SLaks
Yes it solve dmy problem.Thank you.
David
If i suppose to use XmlTextReader (not linq to xml) how can i achieve the same result ?
David
It would take a lot more code. LINQ to XML is the easiest way to handle XML in .Net _by far_.
SLaks