XML Deserialization of collection property with code defaults

You are correct that many serializers (though not all) work this way. Json.NET does, its JsonConverter.ReadJson method actually has an Object existingValue for exactly this situation. I don’t know of any documents where these sorts of implementation details are spelled out. The easiest way to determine whether a serializer uses pre-allocated collections when present rather … Read more

How to XML-serialize a dictionary

Take a look at the following blog post http://blogs.msdn.com/b/psheill/archive/2005/04/09/406823.aspx http://web.archive.org/web/20100703052446/http://blogs.msdn.com/b/psheill/archive/2005/04/09/406823.aspx and this one (not in english, but the code is useful) http://huseyint.com/2007/12/xml-serializable-generic-dictionary-tipi/ Code sample from: http://web.archive.org/web/20100703052446/http://blogs.msdn.com/b/psheill/archive/2005/04/09/406823.aspx using System.Collections.Generic; using System.Collections; using System.IO; using System.Xml.Serialization; using System.Xml; using System; public static void Serialize(TextWriter writer, IDictionary dictionary) { List<Entry> entries = new List<Entry>(dictionary.Count); foreach (object key in … Read more

Deserialize XML To Object using Dynamic

You may want to try this. string xml = @”<Students> <Student ID=””100″”> <Name>Arul</Name> <Mark>90</Mark> </Student> <Student> <Name>Arul2</Name> <Mark>80</Mark> </Student> </Students>”; dynamic students = DynamicXml.Parse(xml); var id = students.Student[0].ID; var name1 = students.Student[1].Name; foreach(var std in students.Student) { Console.WriteLine(std.Mark); } public class DynamicXml : DynamicObject { XElement _root; private DynamicXml(XElement root) { _root = root; } … Read more

ShouldSerialize*() vs *Specified Conditional Serialization Pattern

The intent of the {propertyName}Specified pattern is documented in XML Schema Binding Support: MinOccurs Attribute Binding Support. It was added to support an XSD schema element in which: The <element> element is involved. minOccurs is zero. The maxOccurs attribute dictates a single instance. The data type converts to a value type. In this case, xsd.exe … Read more

XML Serialization and namespace prefixes

To control the namespace alias, use XmlSerializerNamespaces. [XmlRoot(“Node”, Namespace=”http://flibble”)] public class MyType { [XmlElement(“childNode”)] public string Value { get; set; } } static class Program { static void Main() { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(“myNamespace”, “http://flibble”); XmlSerializer xser = new XmlSerializer(typeof(MyType)); xser.Serialize(Console.Out, new MyType(), ns); } } If you need to change the namespace … Read more

Memory Leak using StreamReader and XmlSerializer

The leak is here: new XmlSerializer(typeof(XMLObj), new XmlRootAttribute(“rootNode”)) XmlSerializer uses assembly generation, and assemblies cannot be collected. It does some automatic cache/reuse for the simplest constructor scenarios (new XmlSerializer(Type), etc), but not for this scenario. Consequently, you should cache it manually: static readonly XmlSerializer mySerializer = new XmlSerializer(typeof(XMLObj), new XmlRootAttribute(“rootNode”)) and use the cached serializer … Read more