Obsolete attribute causes property to be ignored by XmlSerialization

EDIT: After reading a MS Connect article, it appears that .Net 2.0 has a ‘feature’ where it makes ObsoleteAttribute equivalent to XmlIgnoreAttribute without any notification in the documentation. So I’m going to revise my answer to say that the only way to have your cake and eat it too in this instance is to follow … Read more

Ignore a property during xml serialization but not during deserialization

This is the solution outlined by Manoj: If you want to suppress serialization of a specific property Foo, but still be able to deserialize it, you can add a method public bool ShouldSerializeFoo() that always returns false. Example: public class Circle2 { public double Diameter { get; set; } public double Radius { get { … Read more

Serialize Python dictionary to XML [closed]

There is huTools.structured.dict2xml which tries to be compatible to simplejson in spirit. You can give it hints how to wrap nested sub-structures. Check the documentation for huTools.structured.dict2et which returns ElementTree Objects instead if the strings returned by dict2xml. >>> data = {“kommiauftragsnr”:2103839, “anliefertermin”:”2009-11-25″, “prioritaet”: 7, … “ort”: u”Hücksenwagen”, … “positionen”: [{“menge”: 12, “artnr”: “14640/XL”, “posnr”: … Read more

What is the best way to parse (big) XML in C# Code?

Use XmlReader to parse large XML documents. XmlReader provides fast, forward-only, non-cached access to XML data. (Forward-only means you can read the XML file from beginning to end but cannot move backwards in the file.) XmlReader uses small amounts of memory, and is equivalent to using a simple SAX reader. using (XmlReader myReader = XmlReader.Create(@”c:\data\coords.xml”)) … Read more

“Type not expected”, using DataContractSerializer – but it’s just a simple class, no funny stuff?

The exception that is being reported is for VDB_Sync.Model.Konstant. This means that somewhere further up the chain, this class is being pulled into another class and that class is the one being serialized. The issue is that depending on how Konstant is embedded in this class (for example, if it is in a collection or … 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

Suppress Null Value Types from Being Emitted by XmlSerializer

Try adding: public bool ShouldSerializeAmount() { return Amount != null; } There are a number of patterns recognised by parts of the framework. For info, XmlSerializer also looks for public bool AmountSpecified {get;set;}. Full example (also switching to decimal): using System; using System.Xml.Serialization; public class Data { public decimal? Amount { get; set; } public … Read more

Using DataContractSerializer to serialize, but can’t deserialize back

Here is how I’ve always done it: public static string Serialize(object obj) { using(MemoryStream memoryStream = new MemoryStream()) using(StreamReader reader = new StreamReader(memoryStream)) { DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); serializer.WriteObject(memoryStream, obj); memoryStream.Position = 0; return reader.ReadToEnd(); } } public static object Deserialize(string xml, Type toType) { using(Stream stream = new MemoryStream()) { byte[] data = … Read more