Thursday, December 1, 2011

Deserialize XML file from Reporting Services

I was trying to work with Reporting Services the other day. I have to make a comparison from a legacy application with my own process. Well, I cannot tell many details about it, but I wanted to upload an xml file from RSS to a class. I thought that it would be a good idea after render the file gets its data into a class using serialization. So I ended up doing the following (for this example I am using a silly report):

I create a report with one table from AdventureWorks database, in this case Address.

I saved the report as XML:

 After that I created a file using XSD tool. To do that, just go to the command tool of Visual Studio,

here is the command line
XSD address.xml
That command line is going to create another file named Address.xsd.  Then you can use the following command to create a VB or C# file.

XSD address.xsd /classes /language:vb

Attached the file produce by the command above into your project. I created a simple Console Application and I added to the main the following code:
Dim myReport As New Report
Dim objStreamReader As New StreamReader("C:\Development\temp\Address.xml")       
Dim x As New XmlSerializer(myReport.GetType)
myReport = x.Deserialize(objStreamReader)
I ran it and I got the following error

"Unable to generate a temporary class error CS0030. Cannot convert type Detail_CollectionDetail()() to Detail_CollectionDetail()" This is an image of the error:
I fix it removing an extra dimension in the Detail_CollectionDetail array. Using Quick Replace, I replaced ()()() for ()().
Running the app again, now I do not get error and I got the class populated.

Ok, this the way that I tried because I was curious following that path. It was not because is the best way to do this (maybe it is). You can play with LINQ to do the same, but that is another story.


No comments:

Post a Comment