views:

369

answers:

2

I'm using VS 2008 to generate a Reporting Services report definition. The problem is that whenever I try to load a report definition from a stream I get an error.

I have the following code:

var loaded = XDocument.Load(filePath);
LocalReport ret = new LocalReport();
using (var stream = new MemoryStream())
{
    var writer = new StreamWriter(stream);
    loaded.Save(writer);
    var ret = new LocalReport();
    ret.LoadReportDefinition(stream);

    var r= ret.GetParameters();
}

When the last line is executing it throws LocalProcessingException with the followinf text:

{"The report definition is not valid.  Details: O elemento raiz está em falta."}

The details translate to "root element missing".

What could be wrong?

Edit: The XML definition is correct. The problem lies somewhere after the loading of the definition.

A: 

Have you tried saving the MemoryStream down to a separate file and comparing it to the original?

Matt Greer
Yes and the file is exactly the same. That's why I don't understand.
Megacan
Do you mind sharing the file? `LoadReportDefinition` generally works fine, I've never heard of any issues with it. It's possible the file has some slightly wrong formatting in it that is confusing it.
Matt Greer
I solved the problem by creating a temporary file and loading that instead. Here's the file:http://www.megaupload.com/?d=I9MJKZTF
Megacan
A: 

You must reset the stream back to position 0 before reading it again. Otherwise LoadReportDefinition will start to read from the end of the stream.

var loaded = XDocument.Load(filePath);
LocalReport ret = new LocalReport();
using (var stream = new MemoryStream())
{
    var writer = new StreamWriter(stream);
    loaded.Save(writer);
    writer.Close();
    writer.Position = 0;
    var ret = new LocalReport();
    ret.LoadReportDefinition(stream);

    var r= ret.GetParameters();
}

See CreateMemoryStream() on this page

Matt Boehlig
Tks for the answer. I haven't touched that project in a while, but when I get a chance I'll try that.
Megacan