Ignacio Ignacio - 1 year ago 122
C# Question

Find Elements by Attribute using XDocument

This query seems to be valid, but I have 0 results.

IEnumerable<XElement> users =
(from el in XMLDoc.Elements("Users")
where (string)el.Attribute("GUID") == userGUID.ToString()
select el);

My XML is as follows:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<User GUID="68327fe2-d6f0-403b-a7b6-51860fbf0b2f">
<Key ID="F7000012ECEAD101">


Do you have any clues to shed some light onto this?

Answer Source

change Users in the 2nd line to User. Like this:

    IEnumerable<XElement> users = (from el in XMLDoc.Root.Elements("User")
                                   where (string)el.Attribute("GUID") ==  userGUID.ToString()
                                   select el);

I'm assuming XMLDoc is an XDocument, and not the root element itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download