5,696,038 members and growing! (13,159 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Simplify Reading and Writing of XML Fragments

By notmasteryet

This article shows how to read and write XML fragments using the XmlChunkReader and XmlChunkWriter classes.
XML, C# 2.0, C#, Windows, .NET, .NET 2.0, Visual Studio, Dev

Posted: 8 Aug 2006
Updated: 8 Aug 2006
Views: 12,869
Bookmarked: 10 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 1.89 Rating: 3.14 out of 5
0 votes, 0.0%
1
1 vote, 25.0%
2
1 vote, 25.0%
3
2 votes, 50.0%
4
0 votes, 0.0%
5

Introduction

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))
{
    r.ReadStartElement("items");
    while (r.Read() && r.MoveToContent() == XmlNodeType.Element)
    {
        using(XmlChunkReader chunkReader = new XmlChunkReader(r))
        {
            Item item = (Item)serializer.Deserialize(chunkReader);
            Console.WriteLine(item.ToString());
        }
    }
    r.ReadEndElement();    
}

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;
    w.WriteStartElement("items");
    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"));
    w.WriteEndElement();
}

The library also simplifies the implementation of the IXmlSerializable interface.

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

License

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

notmasteryet



Occupation: Web Developer
Location: United States United States

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Aug 2006
Editor: Smitha Vijayan
Copyright 2006 by notmasteryet
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project