How can I serialize internal classes using XmlSerializer?

From Sowmy Srinivasan’s Blog – Serializing internal types using XmlSerializer: Being able to serialize internal types is one of the common requests seen by the XmlSerializer team. It is a reasonable request from people shipping libraries. They do not want to make the XmlSerializer types public just for the sake of the serializer. I recently … Read more

Convert an org.w3c.dom.Node into a String

You can use a Transformer (error handling and optional factory configuration omitted for clarity): Node node = …; StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(node), new StreamResult(writer)); String xml = writer.toString(); // Use xml …

XML Serialization question – How to Serialize Element, Attribute and Text from One Object

[XmlText], like so: using System; using System.Xml.Serialization; [Serializable, XmlRoot(“myElement”)] public class MyType { [XmlAttribute(“name”)] public string Name {get;set;} [XmlText] public string Text {get;set;} } static class Program { static void Main() { new XmlSerializer(typeof(MyType)).Serialize(Console.Out, new MyType { Name = “foo”, Text = “bar” }); } }

.net XML Serialization – Storing Reference instead of Object Copy

It is not possible with XmlSerializer. You could achieve this with DataContractSerializer using the PreserveObjectReferences property. You may take a look at this post which explains the details. Here’s a sample code: public class Person { public string Name; public Person Friend; } class Program { static void Main(string[] args) { Person p1 = new … Read more

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; } }

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)