views:

317

answers:

1

Hi,

I got a XML schema for which I used xsd.exe to generate a Class FooClass.

I am receiving xml requests from host which I get them from a directory and use the XmlSerializer.Deserialize() to get a Instance of my FooClass.

Well, this worked till now perfectly and it still does, but suddenly I started to get XML files which are larger (about 300KB) and the time which it takes to Deserialize() is not acceptable! Loading the same XML file with XMLTextReader() takes milliseconds and the time to deserialize is about 1 minute and 30 seconds!

So I thought I'll use the XMLReader to read the XML file and build FooClass by myself! But before I start to rework all my code, I would like to ask you if there is a way to use XmlSerializer.Deserialize() which would be faster?

I am not sure if XMLSerializer Assembly would help me much here?

here is my code which will be called in a Loop for each file

using (MemoryStream ms = new MemoryStream(xmldata)
{
   XmlSerializer sz = new XmlSerializer(typeof(FooClass));
   foo = (FooClass)sz.Deserialize(ms);
} 

Thanks in advance, AK

A: 

Have you tried the new DataContractSerializer in .NET 3.5? The way you use it is pretty much the same as XmlSerializer; whether it's any faster I don't know, but it has many other benefits over the XmlSerializer such as serialising private members, not needing a parameterless constructor, being able to ignore properties etc.

using (MemoryStream ms = new MemoryStream(xmldata))
{
    DataContractSerializer s = new DataContractSerializer(typeof(FooClass));
    FooClass c = s.ReadObject(ms) as FooClass;
}

You will need to add the [DataContract] attribute to FooClass's definition, and you don't have to, but I explicitly tell the serialiser which members I want serialising by decorating them with [DataMember].

[DataContract]
public class FooClass
{
    public string IgnoreThisProperty { get; set; }

    [DataMember]
    public string SerialiseThisProperty { get; set; }
}
Andy Shellam
Thank you very much for your reply. I haven't used the DataContractSerializer yet, but I'll try it if I don't find any other way! ;-)The problem is to redesign/Decorate the FooClass! Is there any toold like the xsd.exe which will generate a Class supporting the DataContractSerializer?
Gohlool
If you need to conform to a specific XML schema, DataContractSerializer won't really help you, because it gives you much less control the document structure
Thomas Levesque
Thank you very much for the info!
Gohlool
@Thomas - I agree the DataContractSerializer doesn't allow as much control over the XML, but in a previous comment it doesn't look the poster is using any schemas, so it may be enough. @Gohlool, there's no need to redesign your class, just put [DataContract] above the class definition, and [DataMember] above each property you want to be (de-)serialised. Converting a class from XmlSerializer to DataContractSerializer takes seconds, depending on its complexity and relationships with other classes.
Andy Shellam
Thank you Andy! I'll give try. Cheers
Gohlool