C.J.
C.J.

Reputation: 16081

Linq XML won't select specified xml elements

I'm trying to grab/select certain xml elements that are deeply nested in a .vcxproj file. So far, I'm not having any luck. No matter what I do, my linq query is coming up empty:

private static void CheckProject(FileSystemInfo file)
{
    Console.WriteLine(file.FullName);
    XDocument doc = XDocument.Load(file.FullName);

    var elems = from elem in doc.Descendants()
        where elem.Name == "Link"
        select elem;

        foreach (XElement elem in elems)
    {
        Console.WriteLine(elem.Name);
    }
}

I also tried this linq query, to no avail:

var elems = from elem in doc.Descendants("Link")
        select elem;

Upvotes: 2

Views: 319

Answers (1)

Jean-Philippe Leclerc
Jean-Philippe Leclerc

Reputation: 6805

You need to compare the LocalName, not the XName.

var elems = doc.Descendants().Where(e=> e.Name.LocalName == "Link");
foreach (XElement elem in elems)
{
    Console.WriteLine(elem.Name.LocalName);
}

Upvotes: 2

Related Questions