Using XmlSerializer with an array in the root element

You should use [XmlElement], and not [XmlArray] to decorate the Items property – it’s already an array, and you only want to set the element name. public class StackOverflow_12924221 { [XmlRoot(“scan_details”)] public class ScanDetails { [XmlElement(“object”)] public ScanDetail[] Items { get; set; } } public class ScanDetail { [XmlAttribute(“name”)] public string Filename { get; set; … Read more

How to add attributes for C# XML Serialization

Where do you have the type stored? Normally you could have something like: class Document { [XmlAttribute(“type”)] public string Type { get; set; } [XmlText] public string Name { get; set; } } public class _Filter { [XmlElement(“Times”)] public _Times Times; [XmlElement(“Document”)] public Document Document; }

Serializing a Nullable in to XML

If you just want it to work, then perhaps: using System; using System.ComponentModel; using System.Xml.Serialization; public class Account { // your main property; TODO: your version [XmlIgnore] public Nullable<DateTime> AccountExpirationDate {get;set;} // this is a shim property that we use to provide the serialization [XmlAttribute(“AccountExpirationDate”)] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public DateTime AccountExpirationDateSerialized { get {return AccountExpirationDate.Value;} set … Read more

DataContractSerializer – how can I output the xml to a string (as opposed to a file)

Something like this – put your output into a MemoryStream and then read that back in: public static string DataContractSerializeObject<T>(T objectToSerialize) { using(MemoryStream memStm = new MemoryStream()) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(memStm, objectToSerialize); memStm.Seek(0, SeekOrigin.Begin); using(var streamReader = new StreamReader(memStm)) { string result = streamReader.ReadToEnd(); return result; } } }

Force XML serialization to serialize readonly property

Honestly this doesn’t seem too bad to me as long as it is documented You should probably throw an exception if the setter is actually called: /// <summary> /// Blah blah blah. /// </summary> /// <exception cref=”NotSupportedException”>Any use of the setter for this property.</exception> /// <remarks> /// This property is read only and should not … Read more

Most elegant XML serialization of Color structure

Here’s something I’m using for serializing the Color struct in XML. It’s better than shadowing the primary Color property in my opinion. Any suggestions welcome. The XmlColor class relies primarily on the implicit operator language feature to provide the key data tranformations. Without this, the class is basically useless. Other bits of functionality were added … Read more

Java Serialization vs JSON vs XML

In general the important question is which client will receive the serialized objects – browsers/JavaScript engines like (node-js), Java client, unknown/multiple clients. JSON – JSON syntax is basically JavaScript and therefore any component with a JS engine will handle its parsing very well – even complicated data-structures will be converted to “living” objects efficiently. JSON … Read more