当我尝试将数据序列化为xml时,我正在使用这个有用的类:http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx
那么我有:
public class LocalizableString : SerializableDictionary<string, object>
{
public object Get(string language)
{
return this[language];
}
public void Set(string language, string value)
{
Util.SetDictionaryValue<string, object>(this, language, value);
}
}
如果我使用通常的代码:
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
foreach (TKey key in this.Keys)
{
writer.WriteStartElement("item");
writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement("value");
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
我会得到:
<PersonenListe>
<PersonenArray>
<PersonObjekt>
<PropertyType>Unknown</PropertyType>
<ID>0</ID>
<FriendlyName>Max Man</FriendlyName>
<City>Capitol City</City>
<Age>33</Age>
<Name>
<item>
<key>
<string>test1</string>
</key>
<value>
<anyType xmlns:q1="http://www.w3.org/2001/XMLSchema" p8:type="q1:string" xmlns:p8="http://www.w3.org/2001/XMLSchema-instance">test2</anyType>
</value>
</item>
</Name>
</PersonObjekt>
</PersonenArray>
</PersonenListe>
但是我需要像上面那样使用自动类型定义的代码,但没有任何类型的代码(ony属性类型),如下例所示:
<PersonenArray>
<PersonObjekt>
<PropertyType>Unknown</PropertyType>
<ID>0</ID>
<FriendlyName>Max Man</FriendlyName>
<City>Capitol City</City>
<Age>33</Age>
<Name>
<item>
<key>
<string>test1</string>
</key>
<value>
<string>test2</string>
</value>
</item>
</Name>
</PersonObjekt>
</PersonenArray>
</PersonenListe>
答案 0 :(得分:0)
这是因为您的基础词典是键入的<string, object>
。如果您的键和值始终是字符串,则从
public class LocalizableString : SerializableDictionary<string, object>
到
public class LocalizableString : SerializableDictionary<string, string>
我猜这应该可以解决你的问题。