使用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 联系我们: