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"?>
<Users>
<User GUID="68327fe2-d6f0-403b-a7b6-51860fbf0b2f">
<Key ID="F7000012ECEAD101">
...
</Key>
</User>
</Users>
Do you have any clues to shed some light onto this?
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);
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.
精彩评论