Search code examples
c#.netxmlxelement

Need some help with a XElement Query


This query isn't working as I'm expecting it to. Anyone see the problem?

I'm trying to get an element by it's name, but it's not returning anything. Here is the specific part of the function I need some help with:

alt text


Update

The solution was to use an XName instead of a string. Like so:

var matchingElements = elements.Where(e => e.Name.Equals(XName.Get(name)));

Solution

  • Try changing your line to:

    elements.Where(e => e.Name.LocalName == name) 
    

    The LocalName part is the important part, as otherwise you are comparing equality of an XName with a string. Remember, XML supports names of the style "prefix:element-name". In that example, "prefix" is the identifier associated with the namespace returned by e.Name.Namespace and "element-name" is the identifier returned by e.Name.LocalName.