transformer.setOutputProperty(OutputKeys.ENCODING, “UTF-8”) is NOT working

I had the same problem on Android when serializing emoji characters. When using UTF-8 encoding in the transformer the output was HTML character entities (UTF-16 surrogate pairs), which would subsequently break other parsers that read the data. This is how I ended up solving it: StringWriter sw = new StringWriter(); sw.write(“<?xml version=\”1.0\” encoding=\”UTF-8\” ?>”); Transformer … Read more

Best solution for XmlSerializer and System.Drawing.Color

The simplest method uses this at it’s heart: String HtmlColor = System.Drawing.ColorTranslator.ToHtml(MyColorInstance); It will just convert whatever the color is to the standard hex string used by HTML which is just as easily deserialized using: Color MyColor = System.Drawing.ColorTranslator.FromHtml(MyColorString); That way you’re just working with bog standard strings…

Serializing a list of Key/Value pairs to XML

KeyValuePair is not serializable, because it has read-only properties. Here is more information(thanks to Thomas Levesque). For changing the generated name use the [XmlType] attribute. Define your own like this: [Serializable] [XmlType(TypeName=”WhateverNameYouLike”)] public struct KeyValuePair<K, V> { public K Key { get; set; } public V Value { get; set; } }

Can XmlSerializer deserialize into a Nullable?

I think you need to prefix the nil=”true” with a namespace in order for XmlSerializer to deserialise to null. MSDN on xsi:nil <?xml version=”1.0″ encoding=”UTF-8″?> <entities xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:type=”array”> <entity> <id xsi:type=”integer”>1</id> <name>Foo</name> <parent-id xsi:type=”integer” xsi:nil=”true”/>

C# Xml Serialization & Deserialization

In your deserialization code you’re creating a MemoryStream and XmlTextWriter but you’re not giving it the string to deserialize. using (MemoryStream memStream = new MemoryStream()) { using (XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.Unicode)) { // Omitted } } You can pass the bytes to the memory stream and do away with the XmlTextWriter altogether. using … Read more

XmlSerializer property converter

Treat the node as a custom type: [XmlRoot(“ObjectSummary”)] public class Summary { public string Name {get;set;} public BoolYN IsValid {get;set;} } Then implement IXmlSerializable on the custom type: public class BoolYN : IXmlSerializable { public bool Value { get; set } #region IXmlSerializable members public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(System.Xml.XmlReader reader) … Read more

How can I convert a DataTable to an XML file in C#?

You can use DataTable.WriteXml Method. Here is an example; How can i convert my datatable into XML using C# 2.0? string result; using (StringWriter sw = new StringWriter()) { dataTable.WriteXml(sw); result = sw.ToString(); } If you don’t actually need a string but read-only, processable XML, it’s a better idea to use MemoryStream and XPathDocument: XPathDocument … Read more