Well, the Users elements don't have GUID attributes. Two suggested options:
- Look at
XDocument.Root.Elements("User")
- Use
Descendants("User")
to find all User elements.
I'll stick with the former for the moment. That gives us:
IEnumerable<XElement> users =
(from el in XMLDoc.Root.Elements("User")
where (string) el.Attribute("GUID") == userGUID.ToString()
select el);
Now, we can still tidy this up further. Firstly, let's cast to Guid
instead of string
:
IEnumerable<XElement> users =
(from el in XMLDoc.Root.Elements("User")
where (Guid) el.Attribute("GUID") == userGUID
select el);
However there's not a lot of reason to use a query expression here... all you're applying is a single predicate. Let's just use the Where
method directly:
IEnumerable<XElement> users =
XMLDoc.Root
.Elements("User")
.Where(el => (Guid) el.Attribute("GUID") == userGUID);
How you lay it out is up to you, of course :) With a longer line, you can probably align everything up under a longer first line:
IEnumerable<XElement> users = XMLDoc.Root
. etc
Now, finally - what about if the User element doesn't have a GUID attribute? Currently, this code will throw an exception. That may be exactly what you want - or it may not. If it's not, you can make it ignore such things by casting to Nullable<Guid>
aka Guid?
instead:
IEnumerable<XElement> users =
XMLDoc.Root
.Elements("User")
.Where(el => (Guid?) el.Attribute("GUID") == userGUID);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…