问题:我使用可序列化的字典类,在 http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx 处找到,以序列化字典。 这工作正常,但我遇到了一个恼人的问题。可串行化字典,如何设置键名?
<System.Xml.Serialization.XmlRoot("DataBase")> _
Public Class cDataBase
<System.Xml.Serialization.XmlNamespaceDeclarations()> _
Public ns As New System.Xml.Serialization.XmlSerializerNamespaces()
<System.Xml.Serialization.XmlElement("Tables")> _
Public Tables1 As New SerializableDictionary(Of String, cTable)
End Class ' cDataBase
当我序列化上面的类的实例,因为这, 创建的XML看起来是这样的:
<Tables>
<item>
<key>
<string>MyTable</string>
</key>
<value>
<Table CreationDate="0001-01-01T00:00:00" LastModified="0001-01-01T00:00:00">
<Columns>
<Column Name="PrimaryKeyName">
<DataType>Uniqueidentifier</DataType>
<Length>36</Length>
</Column>
</Columns>
<Rows>
<Row>
<Item>Reihe1</Item>
<Item>Reihe2</Item>
<Item>Reihe3</Item>
</Row>
<Row>
<Item>Reihe1</Item>
<Item>Reihe2</Item>
<Item>Reihe3</Item>
</Row>
这将是很好的,如果我能想出如何重新命名的关键 <串>到在属性定义
<key>
<string>MyTable</string>
</key>
基本上类似的东西XmlArrayItem属性,如下面,如果头(只)的字典是一个数组...
<System.Xml.Serialization.XmlArray("Tables")> _
<System.Xml.Serialization.XmlArrayItem("Table")> _
Public Tables As New List(Of cTable)
我想尝试的字符串改变为从字符串继承的自定义类,我可以用一个名称装备,但问题是,一个不能从字符串继承...
首先,我限于.NET 2.0,和第二,主要的问题是,字典是一个通用型和不可序列(而不是在任一JSON)。我正在序列化为XML,因为XML使用更广泛,如果从.NET 2.0序列化,则更好阅读。 – 2010-07-30 11:21:56