public override void ReadXml(XmlReader reader) { BuildAccessorsInfo(); // Box the struct value now to make it mutable object boxed = (TStruct)m_defaultValueField.GetValue(null); reader.MoveToElement(); if (reader.IsEmptyElement) { reader.Skip(); return; } reader.ReadStartElement(); reader.MoveToContent(); while (reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.None) { if (reader.NodeType == XmlNodeType.Element) { Accessor accessor; if (m_accessorMap.TryGetValue(reader.LocalName, out accessor)) { object value; if (accessor.IsPrimitiveType) { string valueStr = reader.ReadElementString(); TypeConverter converter = TypeDescriptor.GetConverter(accessor.Type); value = converter.ConvertFrom(null, CultureInfo.InvariantCulture, valueStr); } else if (accessor.SerializerType != null) { var serializer = Activator.CreateInstance(accessor.SerializerType) as IMyXmlSerializable; serializer.ReadXml(reader.ReadSubtree()); value = serializer.Data; reader.ReadEndElement(); } else { XmlSerializer serializer = MyXmlSerializerManager.GetOrCreateSerializer(accessor.Type); string rootName = MyXmlSerializerManager.GetSerializedName(accessor.Type); value = Deserialize(reader, serializer, rootName); } accessor.SetValue(boxed, value); } else { reader.Skip(); } } reader.MoveToContent(); } reader.ReadEndElement(); // Unbox the mutated struct value m_data = (TStruct)boxed; }
public void WriteXml(XmlWriter writer) { Type type = m_data.GetType(); XmlSerializer serializer = MyXmlSerializerManager.GetOrCreateSerializer(type); var derivedName = MyXmlSerializerManager.GetSerializedName(type); CustomRootWriter customWriter; WriterPool.AllocateOrCreate(out customWriter); customWriter.Init(derivedName, writer); serializer.Serialize(customWriter, m_data); customWriter.Release(); WriterPool.Deallocate(customWriter); }