Click here to Skip to main content
14,700,221 members
Articles » Languages » C / C++ Language » General
Posted 8 Aug 2006


19 bookmarked

Simplify Reading and Writing of XML Fragments

Rate me:
Please Sign up or sign in to vote.
3.35/5 (6 votes)
8 Aug 2006
This article shows how to read and write XML fragments using the XmlChunkReader and XmlChunkWriter classes.


This article is trying to solve two ultimate serialization problems:

  • Can an XML stream be deconstructed using multiple nested XmlReader processes;
  • Can an XML stream be assembled from multiple nested XmlWriter processes.

Solving these problems makes the using of XmlSerializer possible when you are creating XML from objects.

Using the Code

The library contains two classes: XmlChunkReader and XmlChunkWriter.

XmlChunkReader can be created using XmlReader while it’s reading XML element nodes. It can be used as a standalone XmlReader, e.g., to perform a deserialization operation. When XmlChunkReader is closed, the main reader points to the next XML node. Using XmlChunkReader helps avoid such calls as ReadInnerXml.

// read file
using (XmlTextReader r = new XmlTextReader(xmlfile))
    while (r.Read() && r.MoveToContent() == XmlNodeType.Element)
        using(XmlChunkReader chunkReader = new XmlChunkReader(r))
            Item item = (Item)serializer.Deserialize(chunkReader);

XmlChunkWriter can be created using XmlWriter. It can be used as a standalone XmlWriter, e.g., to perform serialization of objects. When XmlChunkWriter is closed, the main XmlWriter is ready to continue writing XML data. Using XmlChunkWriter helps format data in the same manner and avoids writing of XML declaration instructions in the middle of XML.

// write file
using (XmlTextWriter w = new XmlTextWriter(xmlfile, Encoding.UTF8))
    w.Formatting = Formatting.Indented;
    using (XmlChunkWriter chunkWriter = new XmlChunkWriter(w))
        serializer.Serialize(chunkWriter, new Item(1, "First"));

    using (XmlChunkWriter chunkWriter = new XmlChunkWriter(w))
        serializer.Serialize(chunkWriter, new Item(2, "Second"));

The library also simplifies the implementation of the IXmlSerializable interface.

The source code can be easily adapted for .NET framework version 1.1.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer
United States United States
No Biography provided

Comments and Discussions

QuestionI made a couple of changes to get this to work with the serializers generated by .NET 4.5.1 Pin
Bern McCarty13-Dec-13 2:05
MemberBern McCarty13-Dec-13 2:05 
GeneralGreat, just what I need! Pin
Rynus_Rein3-Jun-09 0:17
MemberRynus_Rein3-Jun-09 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.