代码块四(如下)给了我一个错误,我在修复方面不知所措......
这是我正在使用的 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
尝试检索与缓存名称对应的缓存模式(在本例中为客户)
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 的第一天,我似乎对如何访问该属性有一个基本的误解 - 查询似乎正在返回一个集合,我知道只能找到一个可能的值......