2012-10-03 54 views
1

代碼塊四(下)選擇一個元素時返回一個屬性是給我的,我在關於固定損失的錯誤...的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合作的第一天,我似乎對如何訪問屬性有一個基本的誤解 - 看起來查詢正在返回一個集合,並且我知道只有一種可能值,可以發現...

+0

我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

0

前三代碼塊都達到同樣的效果,但略有不同的方法來訪問屬性的內容:

1.「加密的」屬性的第一個(只)實例的直接返回:

Return "true" = (From DBConnections In Configurations.<dbConnection> Where "Customization" = [email protected] Select DBConnections.<password>[email protected]).Single 

2.曲ERY XElements的集合,然後拉離第一的XElement方法的「加密」屬性:

Dim Element As IEnumerable(Of XElement) = (From DBConnections In Configurations.<dbConnection> Where "Customization" = [email protected] Select DBConnections.<password>).Single 
Return "true" = Element(0)[email protected] 

前面的例子輕輕誤導我們,因爲參考。單明確規定,將有一個且只有一個發現 - 但實際上我們必須處理一個集合的結果(只有一個項目)。與方法1相比,它更令人困惑,我們不必明確處理集合來檢索屬性值。 a)

3a。該查詢從集合,然後拉「加密」屬性的方法第一的XElement:

Dim Element As XElement = ((From DBConnections In Configurations.<dbConnection> Where "Customization" = [email protected] Select DBConnections.<password>).Single)(0) 
Return "true" = [email protected] 

最後一個例子努力應對必須處理一個集合(一個項目)的誤導性的一部分,該通過引用集合中的第一個項目並儘可能快地檢索集合中的項目,並將其存儲在適當的類型中,以便我們不必擔心集合,而是處理我們真正感興趣的對象(在此case XElement對象)。

3b。第一的XElement對象

Dim Element As XElement = ((From DBConnections In Configurations.<dbConnection> Where "Customization" = [email protected] Select DBConnections.<password>).Single)(0) 
If Value Then 
    [email protected] = "true" 
Else 
    [email protected] = "false" 
End If 

通過三個查詢工作中改變「加密」屬性辦法幫助澄清關於什麼是由LINQ返回我的心智模式。此外,我需要了解這裏修改「加密」屬性的真實情況......