0

Possible Duplicate:
Understanding Linq To Xml - Descendants return no results

So I've been looking at the Microsoft example:

http://msdn.microsoft.com/en-us/library/bb387061.aspx

There, they do like this:

IEnumerable<string> partNos =
from item in purchaseOrder.Descendants("Item")
select (string) item.Attribute("PartNumber");

They use "Descendants" to address an item in purchaseOrder which is actually 3 levels deep.

Now, when I try to do the same thing with my XML, I get nothing.

My XML:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>Roulette</name>

<modules>
    <module>application</module>
    <module>test</module>
</modules>

My code:

XDocument mainPOM = XDocument.Load(above_xml);
List<string> pomLocations = (from loc in mainPOM.Descendants("module") select (string)loc.Name.LocalName).ToList();
Console.WriteLine(pomLocations.Count);

Unfortunately, pomLocations has a length of 0 :(.

Can somebody please tell me where exactly am I messing up?

Community
  • 1
  • 1
Axonn
  • 10,076
  • 6
  • 31
  • 43

1 Answers1

1

Your root element contains this:

xmlns="http://maven.apache.org/POM/4.0.0"

That's setting the default namespace for descendant elements and itself. So the name of the element isn't just "project" - it's "project" within that namespace. You want:

XNamespace ns = "http://maven.apache.org/POM/4.0.0";
var locations = mainPOM.Descendants(ns + "project")
                       .Select(...);

I've left the Select clause as "..." as I don't think you really want loc.Name.LocalName, which would always be "project" by virtue of the query.

Additionally, it's not clear you really want Descendants anyway - if project is the root element, why not just use mainPOM.Root?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi Jon, thanks for answering! :). Actually, I want to select the inside of the node. But that was temporary code, just to select *something*, anything. And BTW, it doesn't work anyway. I just added the ns in my existing code and length is still 0 ::- /. Ideas? As for Descendants. I want it because I actually am looking for the "module" node. Again, it was just some test code. I will edit it. – Axonn Nov 02 '12 at 13:18
  • Ok, now it works. I replaced "project" with "module". It seems ns + "project" returns 0 items. ns + "module" returned the expected 2. Thank you. – Axonn Nov 02 '12 at 13:24
  • @Axonn: Well what is `mainPOM`? If it's already the `project` element, that would explain it. This is where providing a short but *complete* program demonstrating the program helps - you've left us guessing, basically. – Jon Skeet Nov 02 '12 at 13:35
  • Yeah, the `mainPOM` is already the `project` element. As I said, I provided my full XML. I will edit my question and also show that mainPOM is that XML :). – Axonn Nov 02 '12 at 13:37
  • @Axonn: You didn't provide the full XML - it doesn't have a closing ``. Additionally, you didn't say the type of `mainPOM`. If it had been `XDocument`, it would be fine - as the root element is a descendant of the document itself. As `XElement`, however, it's not fine, as an element is not a descendant of itself. It's better with the edit, but would be even better still as a short but complete program. – Jon Skeet Nov 02 '12 at 13:42
  • Well, it *is* a complete program. There's nothing else to it than the selection :D. Anyway, I'll do further edits now. – Axonn Nov 02 '12 at 14:05
  • @Axonn: No, it's not a complete program. My definition of "complete" is "I can copy and paste into an empty text file, run the compiler, and execute the results to demonstrate the problem". (For this kind of question I'm fine with also creating an XML file, of course.) Your code doesn't declare a class or a method - it's not a complete program. – Jon Skeet Nov 02 '12 at 14:09
  • You're of course right, but when I say it's a complete program, I assume that anybody who starts Visual Studio and creates a new project can easily paste that, even if they're going to do it in 2 or 3 steps. And for such a simple problem, the rest isn't really relevant. Pasting here extra lines for class declaration & imports & whatnot is not what I would call concise. – Axonn Nov 02 '12 at 14:12