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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 10/22/04


Date: Fri, 22 Oct 2004 11:25:01 -0400

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]
               - mvp@spam.guard.caspershouse.com
"Anders Borum" <a@b.dk> wrote in message 
news:uQwLWoEuEHA.2536@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
> --
>
> 


Relevant Pages

  • Re: XmlTextWriter without a file
    ... > having to craete a physical file. ... You can also open an XmlTextWriter on a MemoryStream and create the XML ... Or you can ceate an XmlTextWriter on a StringWriter and simply read out ...
    (microsoft.public.dotnet.xml)
  • Re: How to write XML to a string
    ... One option it to use the StringWriter as the base for your XmlTextWriter ... (use the XmlTextWriter contructor that takes a TextWriter as its parameter). ... After your are done with all your XML writing, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: XmlTextWriter.WriteElementString problem
    ... StringWriter writerString = new StringWriter; ... XmlTextWriter writer = new XmlTextWriter; ... I am employing the XmlTextWriter class to generate an XML document. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Serialize a class adding a stylesheet
    ... Serialize your object to a string instead of serializing your object ... parameter (StringWriter derives from TextWriter). ... Create an empty XmlDocument object and load Xml from the StringBuilder. ... Use the CreateProcessingInstruction method to create your stylesheet ...
    (microsoft.public.dotnet.framework)
  • Re: Serialization Question
    ... Note that this option can get tricky, as you are going to return an XML string from the web service method, which may not work for them. ... Create another object that has the clients format and transfer the information from your object to that object when the web routine that this client uses is hit. ... The only reason I can see is you are trying to serialize this information and present it, via some sort of service boundary, as XML, not as a serialized object. ... I would take the XML through a transformation to get the format you desire rather than attempt to have the object serialize differently. ...
    (microsoft.public.dotnet.languages.vb)