Bookmark and Share

Thursday, October 15, 2009

Serialize and Deserialize RDL class class to\from XML

If you have a ReportViewer class generated from the XSD report definition file using:
xsd.exe /c /namespace:Rdl ReportDefinition.xsd

You can serialize and deserialize the class to/from RDLC XML:

xmldoc contains the XML RDLC code and is an XmlDocument.

Deserialization, from XML to Class

Rdl.Report report = new Rdl.Report();
XmlSerializer serializer = new XmlSerializer(typeof(Report));
XmlNodeReader xmlr = new XmlNodeReader(xmldoc);
report = (Rdl.Report)serializer.Deserialize(xmlr);

Now you can change the report elements using the objects and collections inside the
Rdl.Report class.

And Serialization, from Class to XML:

XmlDocument xmldoc = new XmlDocument();
StringBuilder sb = new StringBuilder();
MyStringWriterWithEncoding sw = new MyStringWriterWithEncoding(sb, System.Text.Encoding.UTF8);
serializer.Serialize(sw, report);
string sxml = sb.ToString();

Here is a link to the ReportDefinition.cs class: