的LINQ to XML轴属性使用不同的属性
代码块四(下)选择一个元素时返回一个属性是给我的,我在关于固定损失的错误...的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合作的第一天,我似乎对如何访问属性有一个基本的误解 - 看起来查询正在返回一个集合,并且我知道只有一种可能值,可以发现...
你需要.First()
或.Single()
:
Return (From Cache In ConfigurationRoot.<cacheControl>.<cache> Where [email protected] = "Customer" Select [email protected]).First()
谢谢,这是我需要做的。 – Neoheurist
前三代码块都达到同样的效果,但略有不同的方法来访问属性的内容:
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返回我的心智模式。此外,我需要了解这里修改“加密”属性的真实情况......
我编辑了您的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –