使用XDocument属性查找元素。

[英]Find Elements by Attribute using XDocument


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

这个查询似乎是有效的,但是我有0个结果。

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

My XML is as follows:

我的XML如下:

<?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?

你有什么线索可以解释这个吗?

2 个解决方案

#1


27  

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

将第2行中的用户更改为用户。是这样的:

    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.

我假设XMLDoc是一个XDocument,而不是根元素本身。

#2


43  

Well, the Users elements don't have GUID attributes. Two suggested options:

用户元素没有GUID属性。两个建议选项:

  • Look at XDocument.Root.Elements("User")
  • 看看XDocument.Root.Elements(“用户”)
  • 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:

现在,我们还可以进一步整理。首先,让我们转换为Guid而不是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:

但是这里没有太多的理由使用查询表达式…你所应用的只是一个谓词。让我们直接用Where方法:

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:

现在,最后——如果用户元素没有GUID属性怎么办?目前,该代码将抛出异常。这可能正是你想要的——也可能不是。如果它不是,你可以让它忽略这样的东西,将它转换成可空的 aka Guid?而不是:

IEnumerable<XElement> users = 
    XMLDoc.Root
          .Elements("User")
          .Where(el => (Guid?) el.Attribute("GUID") == userGUID);

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/04/20/2fcb9fb28eeba5bac8410df122ffa8fe.html



 
  © 2014-2022 ITdaan.com 联系我们: