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();
xmldoc.LoadXml(sxml);

Here is a link to the ReportDefinition.cs class:

8 comments:

  1. Hey,

    "File not found" on the linkClick.

    ReplyDelete
  2. Hello, thank you for your comment, I modified the wrong link, now it is valid and points to google docs

    ReplyDelete
  3. Hello, This article address exactly what I am trying to do however I get an exception on the deserialize of the rdlc file. It does not expect the first line: '. I am downloading a new xsd file to generate the partial class but I don't think it will solve the problem. Any suggestions would be much appreciated.

    ReplyDelete
  4. Hello Alan,
    The rdlc file is a valid report file? Does it generate a valid report?
    Have you tried to load the RDLC file in a XML validator?
    Have you tried on other RDLC files?

    ReplyDelete
  5. Thanks for the reply.
    (1) Yes, I can run the report directly from the RDLC file using .ReportPath = Server.MapPath("~\Reporting\" & objReport(0).RDLCName)
    (2) No but will give it a shot.
    (3) Yes, Same results from all reports in current project. The reports are quite simple at this stage - a single table with grouping.
    I have tried to generate the xsd directly from an XML view of the report spec but it complains about nested classes.

    ReplyDelete
  6. Matteo, I tried a validator and the only warning was the omission of a DOCTYPE specification. The deserialize complains that the first line ( Report xmlns="http://schemas.microsoft.com/s.... ) was unexpected. I am using VS 2008.

    ReplyDelete
  7. Hello Alan,
    I think that your problem isn't RDLC specific but it is connected to namespaces and deserialization.
    Look at the XmlSerialize constructor, define the default namespac, work on this and I hope you'll solve the problem.
    Here is the link:
    http://msdn.microsoft.com/en-us/library/kw0f5wee(v=VS.80).aspx

    and look also to this:
    http://bytes.com/topic/net/answers/178268-xmlserializer-deserialize-complain-when-root-declare-namespace

    It's a problem really similar to yours.

    Have a nice day and... let me know!

    ReplyDelete
  8. Matteo, That appear to be exactly the problem. Thanks. Alan

    ReplyDelete