代碼塊四(下)選擇一個元素時返回一個屬性是給我的,我在關於固定損失的錯誤...的LINQ to XML軸屬性使用不同的屬性
這裏的XML模式,我使用:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE ctqcfg SYSTEM "cache.dtd">
<cache version="1.0">
<configuration name="Test">
<cacheControl>
<cache name="Customer" mode="off"/>
<cache name="Vendor" mode="off"/>
<cache name="Agency" mode="off"/>
<cache name="Partner" mode="off"/>
</cacheControl>
</configuration>
<configuration name="Production">
<cacheControl>
<cache name="Customer" mode="preload"/>
<cache name="Vendor" mode="dynamic"/>
<cache name="Agency" mode="dynamic"/>
<cache name="Partner" mode="dynamic"/>
</cacheControl>
</configuration>
</cache>
的XML文件被加載
Private XElement As XElement = Nothing
Public Sub Load()
XElement = XElement.Load(ConfigurationResource)
End Sub
當用戶選擇一個配置來編輯,以所選擇的配置的Elemen的根的參考t爲舉行
Private ConfigurationRoot As System.Collections.Generic.IEnumerable(Of System.Xml.Linq.XElement)
Private ConfigurationName_ As String
Public Property ConfigurationName() As String
Get
Return ConfigurationName_
End Get
Set(ByVal Value As String)
ConfigurationName_ = Value
ConfigurationRoot = From Configuration In XElement.<configuration> Where [email protected] = Value
End Set
End Property
嘗試檢索對應於高速緩存名稱緩存模式(在這種情況下,客戶)
Public Property CustomerCache() As String
Get
Try
Return From Cache In ConfigurationRoot.<cacheControl>.<cache> Where [email protected] = "Customer" Select [email protected]
Catch Exception As Exception
Return Nothing
End Try
End Get
Set(ByVal Value As String)
'ToDo
End Set
End Property
我收到以下錯誤
System.InvalidCastException was caught
Message=Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]' to type 'System.String'.
這是我與LINQ合作的第一天,我似乎對如何訪問屬性有一個基本的誤解 - 看起來查詢正在返回一個集合,並且我知道只有一種可能值,可以發現...
我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –