2011-11-02 71 views
1

我有XML,看起來像這樣:查找節點與「失蹤」的屬性

<Doc> 
    <Thing> 
     <ID type="One">Fred</ID> 
    </Thing> 
    <Thing> 
     <ID>Bill</ID> 
    </Thing> 
    <Thing> 
    <ID type="Two">John</ID> 
    </Thing> 
</Doc> 

我可以寫LINQ找到的類型=「一個」節點。 (或類型=「兩課」)

Dim ThingList As IEnumerable(Of XElement) = _ 
    From el In doc...<Thing>.<ID> _ 
    Where el.Attribute("type").Value = "One" _ 
    Select el 

我怎麼會寫LINQ查詢發現,沒有一個類型的屬性在所有的ID的節點?

回答

3

您應該使用的事實,Attribute()將返回null /什麼,如果沒有這樣的屬性。

在C#中,我會用:

var idsMissingType = doc.Descendants("ID") // Or whatever 
         .Where(x => x.Attribute("type") == null); 

猜測是你想要的VB是:

Dim ThingList As IEnumerable(Of XElement) = _ 
    From el In doc...<Thing>.<ID> _ 
    Where el.Attribute("type") Is Nothing _ 
    Select el