A
Anders Borum
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?
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?
