1,serializableattribute does not exist
In Silverlight, SerializableAttribute is no longer included, but without this tag does not affect serialization. The reason for removing this attribute tag is because a series of attribute tokens that have been used to serialize the XML have been started.
For this change, there is no difficulty, the main is more trouble, need to delete. Of course, if you do not want to delete, the easiest way is to create a serializableattribute yourself.
2,dictionary XML serialization, using XElement instead of XmlDocument, convert.changetype parameter changes
Speaking, it is not in Silverlight, there is no way to use XmlSerializer to serialize dictionary<>. And I directly implemented a Dictionary that can be serialized in XML, the code is as follows:
public class Xmlserdictionary<tkey, tvalue>: Dictionary<tkey, Tvalue>, IXmlSerializable
{
#region IXmlSerializable Members
System.Xml.Schema.XmlSchema Ixmlserializable.getschema ()
{
return null;
}
void Ixmlserializable.readxml (System.Xml.XmlReader reader)
{
XmlDocument doc = new XmlDocument ();
Doc. Loadxml (reader. ReadOuterXml ());
foreach (XmlElement item in Doc. childnodes [0]. ChildNodes)
{
TKey key = (TKey) convert.changetype (item. GetAttribute ("key"), typeof (TKey));
Tvalue value = (Tvalue) convert.changetype (item. InnerText, typeof (Tvalue));
This. ADD (key, value);
}
}
void Ixmlserializable.writexml (System.Xml.XmlWriter writer)
{
foreach (var item in this)
{
Writer. WriteStartElement ("item");
Writer. WriteAttributeString ("Key", item. Key.tostring ());
Writer. WriteValue (item. Value);
Writer. WriteEndElement ();
}
}
#endregion
}