Back to Devexpress

XtraReport.FromStream(Stream, Boolean) Method

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-xtrareport-dot-fromstream-x28-system-dot-io-dot-stream-system-dot-boolean-x29.md

latest10.8 KB
Original Source

XtraReport.FromStream(Stream, Boolean) Method

Loads the report definition data from the specified stream and creates a report object from it. The created report’s class is also specified in the REPX data format.

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public static XtraReport FromStream(
    Stream stream,
    bool loadState = true
)
vb
Public Shared Function FromStream(
    stream As Stream,
    loadState As Boolean = True
) As XtraReport

Parameters

NameTypeDescription
streamStream

The Stream object containing the REPX or XML data to load.

|

Optional Parameters

NameTypeDefaultDescription
loadStateBooleanTrue

true to load the report’s state that is also saved in the REPX or XML data; otherwise, false.

|

Returns

TypeDescription
XtraReport

An XtraReport class or its descendant of the type specified in the REPX or XML data.

|

Remarks

Note

DevExpress Reports default configuration prohibits CodeDOM deserialization. CodeDOM deserialization can trigger execution of malicious code. That code can either be directly embedded in the report definition or contained in an external assembly referenced by the report. If you trust the report’s source, you can set Settings.AllowCodeDomLayoutDeserialization to true at application startup to allow CodeDOM deserialization.

To avoid this, we recommend that you use XML serialization instead of CodeDOM (so that reports can be safely deserialized using the XtraReport.LoadLayoutFromXml method instead of the less secure LoadLayout method) and prevent any untrusted third-party libraries from being available on the server.

The CodeDOM serialization is not supported under the Full Trust permission level, and XML serialization is the only option to save reports.

Use this method to create a report instance from the report definition data stored in the specified stream. Note that to be able to create the report, this method should find the class type of the report being created (the report class type name is stored in the REPX data header). This type is searched for in the following assemblies:

  1. The assembly (the .EXE or .DLL file) that produced the report definition data. Its path is also mentioned in the report definition’s header.
  2. The current assembly where the FromStream method is called.
  3. The assemblies referenced by the current assembly.
  4. The loaded assemblies.

If this class type is not found, an instance of the XtraReport class is created.

The saved state can be applied to the created report instance if the loadState parameter is set to true.

Example

This example demonstrates how to save a report definition to a stream (assigned to a corresponding Session object) as a REPX file and restored back by using the XtraReport.SaveLayout and XtraReport.FromStream methods.

csharp
using System.IO;
using DevExpress.XtraReports.UI;
// ...

private void StoreReport(XtraReport report) {
   // Create a stream.
   MemoryStream stream = new MemoryStream();

   // Save a report to the stream.
   report.SaveLayout(stream);

   // Save the stream to a session.
   Session["report_stream"] = stream;
}

private XtraReport RestoreReport() {
   // Restore the stream from the session.
   MemoryStream stream = (MemoryStream)Session["report_stream"];

   // Create a report from the stream.
   return XtraReport.FromStream(stream, true);
}
vb
Imports System.IO
Imports DevExpress.XtraReports.UI
' ...

Private Sub StoreReport(report As XtraReport)
   ' Create a stream.
   Dim stream As New MemoryStream()

   ' Save a report to the stream.
   report.SaveLayout(stream)

   ' Save the stream to a session.
   Session("report_stream") = stream
End Sub

Private Function RestoreReport() As XtraReport
   ' Restore the stream from the session.
   Dim stream As MemoryStream = CType(Session("report_stream"), MemoryStream)

   ' Create a report from the stream.
   Return XtraReport.FromStream(stream, True)
End Function

The following code snippets (auto-collected from DevExpress Examples) contain references to the FromStream(Stream, Boolean) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

reporting-web-mvc-viewer-clustering/CS/DevExpressWebFarmsWebGardenReporting/ReportStorageWebExtension1.cs#L28

csharp
using(var fileStream = File.OpenRead(Path.Combine(workingDirectory, url))) {
    var report = XtraReport.FromStream(fileStream, true);
    return SerializeReport(report);

reporting-wpf-report-storage/CS/WpfApplication38/Storages/DataSetFileStorage.cs#L186

csharp
{
    return XtraReport.FromStream(ms, true);
}

reporting-wpf-mvvm-show-report-document-preview/CS/Models/ReportCatalogViewModel.cs#L41

csharp
using(var stream = new MemoryStream(reportItem.Layout)) {
    return XtraReport.FromStream(stream);
}

reporting-winforms-store-reports-in-a-database/CS/Form1.cs#L57

csharp
sw.Flush();
    newReport = XtraReport.FromStream(sw.BaseStream, true);
}

reporting-winforms-custom-report-storage/CS/Form1.cs#L44

csharp
using (MemoryStream stream = new MemoryStream(Program.ReportStorage.GetData(url))) {
    return XtraReport.FromStream(stream, true);
}

reporting-web-mvc-viewer-clustering/VB/DevExpressWebFarmsWebGardenReporting/ReportStorageWebExtension1.vb#L30

vb
Using fileStream = File.OpenRead(Path.Combine(workingDirectory, url))
    Dim report = XtraReport.FromStream(fileStream, True)
    Return SerializeReport(report)

reporting-wpf-report-storage/VB/WpfApplication38/Storages/DataSetFileStorage.vb#L163

vb
Using ms As New MemoryStream(row.Buffer)
    Return XtraReport.FromStream(ms, True)
End Using

reporting-wpf-mvvm-show-report-document-preview/VB/Models/ReportCatalogViewModel.vb#L56

vb
Using stream = New MemoryStream(reportItem.Layout)
    Return XtraReport.FromStream(stream)
End Using

reporting-winforms-store-reports-in-a-database/VB/Form1.vb#L55

vb
sw.Flush()
    newReport = XtraReport.FromStream(sw.BaseStream, True)
End Using

reporting-winforms-custom-report-storage/VB/Form1.vb#L50

vb
Using stream As New MemoryStream(Program.ReportStorage.GetData(url))
    Return XtraReport.FromStream(stream, True)
End Using

See Also

Store Report Layouts

XML Serialization

XtraReport Class

XtraReport Members

DevExpress.XtraReports.UI Namespace