473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Regarding performance on objects serialized to Xml - thoughts and code presented

Hello Jon, et all.

I am working on a framework with context bound objects (models). The objects
expose common functionality, such as the ability to get a serialized Xml
representation of an object (and its hierarchy if available).

In order to guarantee that wellformed Xml, I use an XmlTextWriter on top of
a StringWriter. I didn't even look at a custom serialization service, as
that would be like reinventing the wheel (atleast thats how I looked at it).

Ok, great so far - but when serializing larger amounts of objects (e.g. 256
or more), the performance is not what I had expected (please note that it is
not the object navigation that is slow, it's the serialization).

I know that programmers would typically cache the generated Xml, but
performance is very important to me and I don't want to cut any corners in a
framework.

For flexibility, I have been thinking about using an abstract pattern,
defining an abstract ObjectSerializerBase then providing a concrete
ElementObjectSerializer etc. for each object type provided by the API. This
would allow a programmer to provide his own concrete serializer to the
serialization, resulting in a quicker process and smaller Xml fragment

Some initial thoughts on the abstract code is presented in the end of this
posting.

Any suggestions to how I could improve the performance in the following code
(aside from the abstract)? I am looking for constructive critique, so don't
hold your breath :-)

C#
public string ToXml()
{
StringWriter sW = new StringWriter();
XmlTextWriter XmlW = Xml.XmlTextWriter(sW);

ToXml(XmlW);

XmlW.Close();
return sW.ToString();
}

internal protected void ToXml(XmlTextWriter XmlW)
{
XmlW.WriteStartElement("Element");
{
XmlW.WriteAttributeString("ElementID", elementID);
etc.

XmlW.WriteElementString("ElementName", elementName);
etc.

XmlW.WriteStartElement("ElementXml");
{
XmlW.WriteRaw(ElementXml);
}
XmlW.WriteEndElement();
}

// Serialize Areas (using existing XmlTextWriter)
Areas.ToXml(XmlW);

// Serialize PlaceHolders (using existing XmlTextWriter)
PlaceHolders.ToXml(XmlW);

XmlW.WriteEndElement();
}

public string ToXml()
{
StringWriter sW = new StringWriter();
XmlTextWriter XmlW = Xml.XmlTextWriter(sW);

ToXml(XmlW);

XmlW.Close();
return sW.ToString();
}

internal protected void ToXml(XmlTextWriter XmlW)
{
XmlW.WriteStartElement("Element");
{
XmlW.WriteAttributeString("ElementID", elementID);
etc.

XmlW.WriteElementString("ElementName", elementName);
etc.

XmlW.WriteStartElement("ElementXml");
{
XmlW.WriteRaw(ElementXml);
}
XmlW.WriteEndElement();
}

// Serialize Areas (using existing XmlTextWriter)
Areas.ToXml(XmlW);

// Serialize PlaceHolders (using existing XmlTextWriter)
PlaceHolders.ToXml(XmlW);

XmlW.WriteEndElement();
}

...

Here's how an abstract version could look like:

C#
// Base class
public abstract ObjectSerializerBase
{
public abstract void ToXml(XmlTextWriter XmlW, CmsObjectNode o);
}

// Concrete serializer class for the type Element
public class ElementObjectSerializer : ObjectSerializerBase
{
public override void ToXml(XmlTextWriter XmlW, CmsObjectNode o)
{
Element e = (Element) o;

XmlW.WriteStartElement("Element");
{
.. include only the necessary attributes / elements
}
XmlW.WriteEndElement();
}
}

// This would be an overloaded method on all objects
public string ToXml(ObjectSerializerBase o)
{
StringWriter sW = new StringWriter();
XmlTextWriter XmlW = Xml.XmlTextWriter(sW);

o.ToXml(XmlW, this);

XmlW.Close();
return sW.ToString();

}

Any ideas? :-)

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #1
3 1726
This code snippet doesn't contain the redundant functions in the original
e-mail. I'm sorry if that confused the code listing a little. This should be
used as reference.

C#
public string ToXml()
{
StringWriter sW = new StringWriter();
XmlTextWriter XmlW = Xml.XmlTextWriter(sW);

ToXml(XmlW);

XmlW.Close();
return sW.ToString();
}

internal protected void ToXml(XmlTextWriter XmlW)
{
XmlW.WriteStartElement("Element");
{
XmlW.WriteAttributeString("ElementID", elementID);
etc.

XmlW.WriteElementString("ElementName", elementName);
etc.

XmlW.WriteStartElement("ElementXml");
{
XmlW.WriteRaw(ElementXml);
}
XmlW.WriteEndElement();
}

// Serialize Areas (using existing XmlTextWriter)
Areas.ToXml(XmlW);

// Serialize PlaceHolders (using existing XmlTextWriter)
PlaceHolders.ToXml(XmlW);

XmlW.WriteEndElement();
}

...

Here's how an abstract version could look like:

C#
// Base class
public abstract ObjectSerializerBase
{
public abstract void ToXml(XmlTextWriter XmlW, CmsObjectNode o);
}

// Concrete serializer class for the type Element
public class ElementObjectSerializer : ObjectSerializerBase
{
public override void ToXml(XmlTextWriter XmlW, CmsObjectNode o)
{
Element e = (Element) o;

XmlW.WriteStartElement("Element");
{
.. include only the necessary attributes / elements
}
XmlW.WriteEndElement();
}
}

// This would be an overloaded method on all objects
public string ToXml(ObjectSerializerBase o)
{
StringWriter sW = new StringWriter();
XmlTextWriter XmlW = Xml.XmlTextWriter(sW);

o.ToXml(XmlW, this);

XmlW.Close();
return sW.ToString();

}

Any ideas? :-)

--
venlig hilsen / with regards
anders borum
--

Nov 16 '05 #2
Anders,

But you are using a custom serialization service, namely, yours. Why
not use XmlSerializer or the SoapFormatter? Each of these will persist your
object into XML (in different formats, and using different methodologies),
but it will work.

Other than that, I would ditch the XmlTextWriter and use a StringBuilder
instance and add the XML to that. That's probably going to give you the
best performance.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Anders Borum" <a@b.dk> wrote in message
news:uQ**************@TK2MSFTNGP11.phx.gbl...
Hello Jon, et all.

I am working on a framework with context bound objects (models). The
objects
expose common functionality, such as the ability to get a serialized Xml
representation of an object (and its hierarchy if available).

In order to guarantee that wellformed Xml, I use an XmlTextWriter on top
of
a StringWriter. I didn't even look at a custom serialization service, as
that would be like reinventing the wheel (atleast thats how I looked at
it).

Ok, great so far - but when serializing larger amounts of objects (e.g.
256
or more), the performance is not what I had expected (please note that it
is
not the object navigation that is slow, it's the serialization).

I know that programmers would typically cache the generated Xml, but
performance is very important to me and I don't want to cut any corners in
a
framework.

For flexibility, I have been thinking about using an abstract pattern,
defining an abstract ObjectSerializerBase then providing a concrete
ElementObjectSerializer etc. for each object type provided by the API.
This
would allow a programmer to provide his own concrete serializer to the
serialization, resulting in a quicker process and smaller Xml fragment

Some initial thoughts on the abstract code is presented in the end of this
posting.

Any suggestions to how I could improve the performance in the following
code
(aside from the abstract)? I am looking for constructive critique, so
don't
hold your breath :-)

C#
public string ToXml()
{
StringWriter sW = new StringWriter();
XmlTextWriter XmlW = Xml.XmlTextWriter(sW);

ToXml(XmlW);

XmlW.Close();
return sW.ToString();
}

internal protected void ToXml(XmlTextWriter XmlW)
{
XmlW.WriteStartElement("Element");
{
XmlW.WriteAttributeString("ElementID", elementID);
etc.

XmlW.WriteElementString("ElementName", elementName);
etc.

XmlW.WriteStartElement("ElementXml");
{
XmlW.WriteRaw(ElementXml);
}
XmlW.WriteEndElement();
}

// Serialize Areas (using existing XmlTextWriter)
Areas.ToXml(XmlW);

// Serialize PlaceHolders (using existing XmlTextWriter)
PlaceHolders.ToXml(XmlW);

XmlW.WriteEndElement();
}

public string ToXml()
{
StringWriter sW = new StringWriter();
XmlTextWriter XmlW = Xml.XmlTextWriter(sW);

ToXml(XmlW);

XmlW.Close();
return sW.ToString();
}

internal protected void ToXml(XmlTextWriter XmlW)
{
XmlW.WriteStartElement("Element");
{
XmlW.WriteAttributeString("ElementID", elementID);
etc.

XmlW.WriteElementString("ElementName", elementName);
etc.

XmlW.WriteStartElement("ElementXml");
{
XmlW.WriteRaw(ElementXml);
}
XmlW.WriteEndElement();
}

// Serialize Areas (using existing XmlTextWriter)
Areas.ToXml(XmlW);

// Serialize PlaceHolders (using existing XmlTextWriter)
PlaceHolders.ToXml(XmlW);

XmlW.WriteEndElement();
}

..

Here's how an abstract version could look like:

C#
// Base class
public abstract ObjectSerializerBase
{
public abstract void ToXml(XmlTextWriter XmlW, CmsObjectNode o);
}

// Concrete serializer class for the type Element
public class ElementObjectSerializer : ObjectSerializerBase
{
public override void ToXml(XmlTextWriter XmlW, CmsObjectNode o)
{
Element e = (Element) o;

XmlW.WriteStartElement("Element");
{
.. include only the necessary attributes / elements
}
XmlW.WriteEndElement();
}
}

// This would be an overloaded method on all objects
public string ToXml(ObjectSerializerBase o)
{
StringWriter sW = new StringWriter();
XmlTextWriter XmlW = Xml.XmlTextWriter(sW);

o.ToXml(XmlW, this);

XmlW.Close();
return sW.ToString();

}

Any ideas? :-)

--
venlig hilsen / with regards
anders borum
--

Nov 16 '05 #3
Hello!

What a quick reply already!
But you are using a custom serialization service, namely, yours. Why
not use XmlSerializer or the SoapFormatter? Each of these will persist your object into XML (in different formats, and using different methodologies),
but it will work.
I have intentionally provide my own "schema" in the serialization, as I
consider this serialization different - and it's intented use is very
different from the situations where you'd usually resort to the
XmlSerializer or SoapFormater (e.g. when you need core data from the API and
transform it to a navigation structure using Xsl).

I actually tried the XmlSerializer approach and it was a little slower than
the XmlTextWriter approach. Given the abstract model I presented, I think
that would solve my problem with the speed. With a hierarchical navigation
structure, the programmer rarely needs anything but some identifiers and a
few properties from the objects.
Other than that, I would ditch the XmlTextWriter and use a StringBuilder instance and add the XML to that. That's probably going to give you the
best performance.


I realize that this is another option, but I have to guarentee valid Xml and
would accept the performance penaulty if that ment that I didn't have to
come up with my own encoding of illegal chars. I would rather provide some
alternatives as to caching etc.

Thanks for sharing your thoughts!

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Sleeman | last post by:
Hi all, i just spent an hour rifling through code to find the cause of a problem only to find it's an oddity with serialization and recursive objects, so figured I'd post for the next person who...
2
by: joerg | last post by:
Hello, we reached a quite interesting performance-limit while using JDBC type 4 to a DB2 V8.1 database. The DB2 is running on a Intel-based single CPU-Server with Linux OS. What we are doing is...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
4
by: John Q. Smith | last post by:
I'm trying to find out some of the details behind OOP state management with SQL Server. For instance - how long does the session object live on any server? Is it created and destoyed with each...
2
by: Michael Isaacs | last post by:
Is there any way to share an object or variable between applications other than through .NET remoting? Also, if this is the only way, what kind of overhead is there for storage (memory) and speed...
2
by: ljlevend2 | last post by:
I've noticed that System.Runtime.Serialization.Formatters.Binary.BinaryFormatter can take a very long time to Serialize and Deserialize a large array of classes that implement ISerializable. In...
2
by: 1944USA | last post by:
I am re-architecting a C# application written as a multithreaded Windows Service and trying to squeeze every bit of performance out of it. 1) Does the thread that an object is instantiated on...
10
by: Mark Rae | last post by:
Hi, This relates to the previous thread "Disappearing Sessions", but is a bit more generic so I thought I'd start a new thread. This one relates to the storing of objects in Session once only to...
2
by: Ryan | last post by:
My apologies if this is not the forum to post questions regarding .NET Remoting, but I figured WebServices would be the most appropriate forum of the bunch. We're currently completely re-arching...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.