コード ブロック 4 (以下) は、修正に関して途方に暮れているというエラーを表示しています...
私が使用している 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
ユーザーが編集する構成を選択すると、選択した構成要素のルートへの参照が保持されます
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 Configuration.@name = Value
End Set
End Property
キャッシュ名 (この場合は Customer) に対応するキャッシュ モードを取得しようとしています。
Public Property CustomerCache() As String
Get
Try
Return From Cache In ConfigurationRoot.<cacheControl>.<cache> Where Cache.@name = "Customer" Select Cache.@mode
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 を使用する最初の日であり、属性へのアクセス方法に関して基本的な誤解があるようです。クエリがコレクションを返しているようで、見つかる可能性のある値は 1 つしかないことを知っています...