我想设置一个像这样的自定义应用配置元素集合部分
<logSectionGroup>
<logSection name="Testttt">
<properties name ="Pride">
<pathName="TestingLog.txt"/>
<deleteRetention="100"/>
<deleteZeroRetention="5"/>
<wildcard="*.txt"/>
</properties>
<properties name ="Adhoc">
<pathName="blah.txt"/>
<deleteRetention="70"/>
<deleteZeroRetention="3"/>
<wildcard="*.*"/>
</properties>
</logSection>
</logSectionGroup>
这可能吗?属性将是配置元素,而日志部分将是配置元素集合。问题是,我只看到了你可以拥有单个元素的多个实例而不是多个元素的地方。
<Section name="Section1">
<Section name="Section1">
<SubSection name="SubSection1">
<Item name="Item1" />
<Item name="Item2" />
</SubSection>
<SubSection name="SubSection2">
<Item name="Item1" />
<Item name="Item2" />
</SubSection>
</Section>
当您使用GetElementKey()时,您在上面的示例中返回元素“name”,但是如何返回4个不同的元素,如“pathName”“deleteRetention”等。
这是我对PropElement的定义
Public Class PropElement
Inherits ConfigurationElement
<ConfigurationProperty("pathName", IsRequired:=True)> _
Public Property PathName() As String
Get
Return CStr(Me("pathName"))
End Get
Set(ByVal value As String)
Me("pathName") = value
End Set
End Property
<ConfigurationProperty("deleteRetention", DefaultValue:="0", IsRequired:=False)> _
Public Property DeleteRetention() As Integer
Get
Return CStr(Me("deleteRetention"))
End Get
Set(ByVal value As Integer)
Me("deleteRetention") = value
End Set
End Property
<ConfigurationProperty("deleteZeroRetention", DefaultValue:="0", IsRequired:=False)> _
Public Property DeleteZeroRetention() As Integer
Get
Return CStr(Me("deleteZeroRetention"))
End Get
Set(ByVal value As Integer)
Me("deleteZeroRetention") = value
End Set
End Property
<ConfigurationProperty("wildcard", DefaultValue:="*.*", IsRequired:=False)> _
Public Property Wildcard() As String
Get
Return CStr(Me("wildcard"))
End Get
Set(ByVal value As String)
Me("wildcard") = value
End Set
End Property
结束班
答案 0 :(得分:1)
也许有点晚了,但你可能还需要解释。 你无法实现这一点,因为XML的本质,你需要一个节点名称。
所以<pathName="TestingLog.txt"/>
无效XML - pathName
节点或属性名称是什么?
但这些可以是属性,所以你会有类似的东西:
<properties name ="Pride" pathName="TestingLog.txt" deleteRetention="100" deleteZeroRetention="5" wildcard="*.txt"/>
然后代码看起来像这样(我希望你能读到C#,但如果没有,请给我留言,然后转换为VB)。
public class PropElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("pathName", IsRequired = true)]
public string PathName
{
get { return (string)this["pathName"]; }
set { this["pathName"] = value; }
}
[ConfigurationProperty("deleteRetention", IsRequired = true)]
public int DeleteRetention
{
get { return (int)this["deleteRetention"]; }
set { this["deleteRetention"] = value; }
}
[ConfigurationProperty("deleteZeroRetention", IsRequired = true)]
public int DeleteZeroRetention
{
get { return (int)this["deleteZeroRetention"]; }
set { this["deleteZeroRetention"] = value; }
}
[ConfigurationProperty("wildcard", IsRequired = true)]
public string Wildcard
{
get { return (string)this["wildcard"]; }
set { this["wildcard"] = value; }
}
}
需要所有属性,但您可以标记它们。
答案 1 :(得分:-1)
当您使用GetElementKey()时,您在上面的示例中返回元素“name”,但是如何返回4个不同的元素,如“pathName”“deleteRetention”等。
每个<property>
元素的类型需要什么,从ConfigurationElement
派生,具有四个属性(定义为元素本身(即每个不同类型的ConfigurationElement
派生自定义类型) - 否则它们将是属性)和一个字符串(您用作集合键)。
然后在<logSection>
的实现中,Properties
集合可以标记为默认集合。
(除了简单的配置结构之外的任何东西都会导致很多类型,但大多数类型都很容易创建。)