Skip to main content

XMLSerialization with C#

When I first hear about XML serialization in C# I thought that it is like Java serialization but the result is XML character stream instead of the default serialization in Java/.net.
But the things are not so great.
The XML serialization in C# is good but it is not the best.
In Java the easiest way to make normal serialization is to implement interface Serializable and to be sure that all fields in your custom class are serializable. If you want to Serialize ArrayList of this objects you dont have problems because ArrayList implements Serializable.

But when you use XML serialization you dont care about Serializable interface. In Java you can make XML serialization this way : http://www.javablogging.com/serialization-to-xml/. You can see that it is simple I thought that in C# the situation is the same but it is not.
To read about serialiization in C# there is tones of info but I didn't found answer like this one in this post:
I will notice only some important aspects of this because when I try C# serialization I have missed some points of this article and i loose 30mins of searching what the problem is.

If you need to serialize ArrayList of strings no problem you can make it this way:
// Serialization
XmlSerializer s = new XmlSerializer( typeof( ArrayList) );
TextWriter w = new StreamWriter( @"c:\list.xml" );
s.Serialize( w, myList );
w.Close();

You can notice that you pass the type of ArrayList to XmlSerializer right? That is the difference.
In Java you don't need to specify what you pass it is "founded" automaticly. But in C# you need to pass all classes that you serialize or I dont know maybe most of all? You have two ways to do this:
If you want to serialize ArrayList of Strings it is ok, but if you want to serialize ArrayList of custom objects lets say Snippets you have to specify this.
For example :
private XmlSerializer serializer = new XmlSerializer(typeof(ArrayList),
new Type[] { typeof(Snippet)});

and if you have a big hierarchy like class A, Class B :A , Class C:A you have to pass all of them. For example:
new XmlSerializer(typeof(ArrayList),
new Type[] { typeof(A),typeof(C),typeof(B) });

if you ask me this is dumb. But wait there is another way and it is explicitly telling the serializer to expect the Snippet class when serializing ArrayList of snippets.
For example :
    // This attribute enables the ArrayList to be serialized:
[System.Xml.Serialization.XmlArray("Snippets")]
// Explicitly tell the serializer to expect the Snippet class
// so it can be properly written to XML from the collection:
[System.Xml.Serialization.XmlArrayItem("snippet",typeof(Snippet))]
public ArrayList listSnippets;

I don't know did you get the idea, but if you ask me Microsoft make something very easy (as you can see in the java example) harder.
But one think I can say to microsoft is that attributes for XML serializations are nice.
What am I talking about ? lets say that we want to edit the Snippet class or we can edit it.
If you leave the default implementation of Snippet:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace CodeSnippetManager
{
public class Snippet
{

public static int counter = 0;

public Snippet ()
{
counter++;
Id = counter;
}

public int Id { get; set; }

private String label;
public String Label {
get
{
return label;
}
set
{
if(value != null && value.Length > 20)
{
value = value.Substring(0, 20);
value.Replace("\r\n", "");
}
this.label = value;
}
}
public String Data { get; set;}
public Snippet(String label,String data)
{
this.Label = label;
this.Data = data;
counter++;
Id = counter;
}

public override string ToString()
{
return Id + ") "+ label;
}
}
}

Btw this class is used in my code snippet manager. So if you keep this and save ArrayList of Snippet objects the result XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<anyType xsi:type="Snippet">
<Id>1</Id>
<Label>ublic static int cou</Label>
<Data>ublic static int counter = 0;</Data>
</anyType>
</ArrayOfAnyType>

But you can use XmlRoot and XMLElement attributes to specify the names of the XML Elements.
If you Snippet class looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace CodeSnippetManager
{
[XmlRoot("SnippetRoot"), Serializable]
public class Snippet
{
[XmlIgnore]
public static int counter = 0;


public Snippet ()
{
counter++;
Id = counter;
}

[XmlElement("SnippetId")]
public int Id { get; set; }

private String label;
[XmlElement("SnippetLabel")]
public String Label {
get
{
return label;
}
set
{
if(value != null && value.Length > 20)
{
value = value.Substring(0, 20);
value.Replace("\r\n", "");
}
this.label = value;
}
}
[XmlElement("SnippetData")]
public String Data { get; set;}
public Snippet(String label,String data)
{
this.Label = label;
this.Data = data;
counter++;
Id = counter;
}

public override string ToString()
{
return Id + ") "+ label;
}

}
}


When you save you get this result :
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<anyType xsi:type="Snippet">
<SnippetId>1</SnippetId>
<SnippetLabel>XmlRoot</SnippetLabel>
<SnippetData>XmlRoot</SnippetData>
</anyType>
</ArrayOfAnyType>


So as a conclusion I can say that XMLSerialization in C# maybe is harder than Java way but you have easy way to customize the result which is very good.

P.S. I've used Serializable in the Snippet class just for readability.

Comments

Popular posts from this blog

Use Multiple JVM versions on Mac OS and Linux

Linux Download multiple Java versions and put them into /opt/ If you already have some JDK from ubuntu repo or etc not a big deal, just fix the paths bellow Register them as alternatives sudo update-alternatives --install /usr/bin/java java /opt/java-8-oracle/bin/java 1081 sudo update-alternatives --install /usr/bin/java java /opt/sap-machine-jdk-11.0.3/bin/java 1080 Edit your ~/.bashrc file alias java11='sudo update-alternatives --set java /opt/sapmachine-jdk-11.0.3/bin/java;export JAVA_HOME=/opt/sapmachine-jdk-11.0.3/' alias java8='sudo update-alternatives --set java /opt/java-8-oracle/bin/java;export JAVA_HOME=/usr/lib/java-8-oracle/' SAVE and start a new bash terminal execute java8 to use java8 java11 to use java11 the latest version you have set stays as system wide, but the JAVA_HOME is not :( you can put java8 or java11 as a last line in the bashrc but since it is sudo it will always require password when start and is not gr...

Hibernate Generic DAO.

When you use Hibernate and DAO pattern it is a good idea to use a Generic Base Dao. The fallowing code snippet contains GenericDAO that is a base class for all my DAO classes. This GenericDAO uses HibernateDaoSupport from Spring for its implementation if you want you can use JpaDaoSupport or JdbcDaoSupport in your projects. My Generic DAO interface looks like this : package org.joke.myproject.dao.base; import java.io.Serializable; import java.util.List; /** * @author Naiden Gochev * @param <E> * @param <PK> */ public interface GenericDao<E,PK  extends Serializable> {     PK save(E newInstance);     void update(E transientObject);     void saveOrUpdate(E transientObject);     void delete(E persistentObject);     E findById(PK id);     List<E> findAll();     List<E> f...

Patching a Maven library with your custom class.

Sometimes you use a library that has a bug. Or maybe it doesn’t has a bug but you want to change something. Of course if it is an open source you can get the sources… build them … with your change and so on. However this first takes a lot of time and second you need the sources. What you usually want .. is to just replace one class.. or few classes with something custom… maybe add a line .. or remove a line and so on. Yesterday… I had an issue with jboss-logging. The version I was using was 3.2.0Beta1 and it turns out that using this version and log4j2 2.0 final basically meant that no log is send to log4j2. The reason was a null pointer exception that was catched in jboss logging class called Log4j2Logger. The bug I submitted is here https://issues.jboss.org/browse/JBLOGGING-107 and it was fixed at the same day. However I will use it as an example since I didn’t knew when this will be fixed.. and I didn’t want to wait till it is fixed. So I was thinking what I want.. to take the j...