0

XMLSerialization with C#

Posted by JOKe on 9/13/2009 06:10:00 PM in
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.

|

0 Comments

Copyright © 2009 JOKe's Blog All rights reserved. Theme based on the Theme by Laptop Geek with changes by JOKe. | Bloggerized by FalconHive.