使用 XDocument 按属性查找元素

发布于 2024-08-29 23:37:36 字数 536 浏览 1 评论 0原文

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

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

我的 XML 如下:

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

</Users>

您有任何线索可以阐明这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

波浪屿的海角声 2024-09-05 23:37:37

将第二行中的Users更改为User。像这样:

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

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

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.

金兰素衣 2024-09-05 23:37:36

嗯,Users 元素没有 GUID 属性。两个建议选项:

  • 查看XDocument.Root.Elements("User")
  • 使用Descendants("User") 查找所有User 元素。

我暂时坚持前者。这给了我们:

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

现在,我们仍然可以进一步整理它。首先,让我们转换为 Guid 而不是 string

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

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

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

当然,如何布局取决于您:) 如果行较长,您可能可以将所有内容对齐在较长的第一行下:

IEnumerable<XElement> users = XMLDoc.Root
                                    . etc

现在,最后 -如果 User 元素没有 GUID 属性怎么办?目前,此代码将引发异常。这可能正是您想要的——也可能不是。如果不是,您可以通过转换为 Nullable 又名 Guid? 来使其忽略此类内容:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文