1
的存在性的的XElement有沒有一種方法,以確定是否含有的XElement任何指定的元素呢?例如,我有我需要檢查XElements:檢查的一個幾種可能XElements
Dim xe1 = <color><blue/></color>
Dim xe2 = <color><red/></color>
Dim xe3 = <color><powderBlue/></color>
Dim xe4 = <color><aqua/></color>
Dim xe5 = <color><green/></color>
我希望能夠查詢任何xelements的,看看他們是否containt <red/>
,<green/>
或<blue/>
它們下面和返回的元素如果是,則返回true,否則返回false。
我希望,這將是simplier,但我能想出的最好的是:
Dim primaryColor = From e In xe1.Elements Where e.Name = "blue" Or e.Name = "red" Or e.Name = "green"
Dim primaryColorTrue = primaryColor.SingleorDefault
If primaryColorTrue IsNot Nothing Then
'Blah
End If
有沒有人有更好的方式來做到這一點,如將紅/綠那些xelements /藍色到數組中,並使用像Elements.Contains(元素列表)的東西?
滑稽stuff-我不喜歡你原來的答案,所以我編碼了,我想到了一個解決方案是更好的,刷新頁面,和你有相同的解決方案我!所以+1。 – RichardOD 2009-11-26 20:33:35
工作就像一個魅力,謝謝!這裏是在VB中 - Dim colors()= New String(){「green」,「blue」,「red」} Dim any As Boolean = el.Descendants.Any(Function(e)colors.Contains(e。 Name.LocalName)) – 2009-11-26 20:35:04
是的,使用元素的東西是一個多用途的夾具,只是一個查詢可能存在或不存在的子元素的樣本。 XML文件不是我的,所以我不能改變它們。 – 2009-11-26 20:39:25