1

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

4

1 回答 1

0

您是否尝试过DataContractSerializer.NET 3.5 中的新功能?您使用它的方式与 ; 几乎相同XmlSerializer。我不知道它是否更快,但它还有许多其他好处,XmlSerializer例如序列化私有成员,不需要无参数构造函数,能够忽略属性等。

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

您需要将[DataContract]属性添加到FooClass的定义中,而您不必这样做,但我通过用[DataMember].

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

    [DataMember]
    public string SerialiseThisProperty { get; set; }
}
于 2010-03-10T12:15:05.800 回答