Saving data to xml - .Net

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,365
Reaction score
346
Location
Pretoria
Which is the best way to saving data to xml in .net 4.0 desktop application?
It might be a general question but I'm not sure which of the following is the better (performance wise) I can use:
xmlWriter
xmlTextWriter
xmlDocument
LINQ to XML
 
Off the cuff, I'd have to go with xmlTextWriter to actually write it, but how you create the XML is a different matter.
 
If you have the data in a clearly defined object structure then none of the above... serialization is the way forward in that case.

All you do is decorate the classes with [Serializable] on the class, and XmlElement and XmlAttribute (or XmlIgnore if you don't want it going to XML) tags... then call a serialise method on an xmlserializer

e.g:

[Serializable]
[XmlRoot(Namespace = "http://www.whatevernamespaceyouwanthere.net")]
public class MyClass
{

[XmlIgnore()]
public string PropertyThatIDontWantSerialized
{
get;
set;
}

[XmlElement(ElementName = "PropertyThatMustBeAnElement")] // You can call it anything here to map the XML name to a different property name on the class
public string PropertyThatMustBeAnElement
{
get;
set;
}

[XmlAttribute(ElementName = "PropertyThatMustBeAnAttribute")] // You can call it anything here to map the XML name to a different name on the class
public string PropertyThatMustBeAnAttribute
{
get;
set;
}
}


Then to serialize and deserialise:

public static void SerialiseToDisk(MyClass myClass, string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

using (FileStream stream = new FileStream(filePath, FileMode.Create))
{
using (TextWriter writer = new StreamWriter(stream))
{
serializer.Serialize(writer, myClass, namespaces);
}
}

}

public static MyClass Deserialize(string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
MyClass result = null;
using (TextReader reader = new StreamReader(filePath))
{
result = serializer.Deserialize(reader) as MyClass;
}
return result;

}
 
Well if you are looking only at performance, then a writer would be quicker, as an xmlserializer uses reflection (which is obviously slower). How much slower though is much of a much. The pain of having to create a writer and form each element is not something that I find particularly fun, so I generally go with serialization where possible.
 
If you are not reading or writing megabytes of data, just take the easiest one and go for it.

If size becomes an issue, you will probably need to go into a bit more depth: Specifically you will need to find out what SAX parsers you can use.

SAX: Very fast, no memory model built.
DOM: Nice to work with, heavy on memory.

But, in general I have found that until my XML files reach about 10MB or more, I just use whatever is easiest.

[Also, just to pre-empt: I'm talking from a Java perspective, haven't touched .NET in years]
 
[Also, just to pre-empt: I'm talking from a Java perspective, haven't touched .NET in years]

Pretty much the same in .Net. Writers will mean you do the plumbing (fast and lightweight)... serialization does the plumbing for you (not as fast or lightweight, probably fine for the job though... what's a few milliseconds between friends?)
 
Well if you are looking only at performance, then a writer would be quicker, as an xmlserializer uses reflection (which is obviously slower). How much slower though is much of a much. The pain of having to create a writer and form each element is not something that I find particularly fun, so I generally go with serialization where possible.
Agreed. The other benefit of serialization is that any changes to the object(s) would immediately also be written to disk, whereas using a writer you'd have to manually update the changes to write to disk as well...

I've done a fairly complex application that writes hundreds of objects to disk and it was very fast. I had to simulate primary keys in databases so that object references still worked 100%, as many objects had references to parent / sibling / child objects and serialization would create circular dependencies if I just referenced the objects directly. So just keep that in mind if you do end up going the serialization route...
 
Top
Sign up to the MyBroadband newsletter
X