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

Convert PFX certificate to JKS, P12, CRT

I recently had to use a PFX certificate for client authentication (maybe another post will be coming) and for that reason I had to convert it to a Java keystore (JKS).  We will create BOTH a truststore and a keystore, because based on your needs you might need one or the other.  The difference between truststore and keystore if you are not aware is(quote from the JSSE ref guide : TrustManager: Determines whether the remote authentication credentials (and thus the connection) should be trusted. KeyManager: Determines which authentication credentials to send to the remote host. Ok that's enough what you will need is openssl and Java 7+ ;) ! First let's generate a key from the pfx file, this key is later used for p12 keystore. openssl pkcs12 -in example.pfx -nocerts -out  example .key   Enter Import Password: MAC verified OK Enter PEM pass phrase: Verifying - Enter PEM pass phrase: As shown here you will be asked for the password of the pfx file, l

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> findAllByProperty(String propertyName,Object value); } All method names are very common so I don't

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