473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Serialization to Stream - how?

Is there any example code I can find which shows how to Serialize an object
to System.IO.Stream.

The reason why is because I want the pass the stream on: The Stream is then
XSLT Transformed by passing the stream to create an XPathDocument object.
Thanks

rizlaroller
Nov 12 '05 #1
4 5897
Here you go,
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyObject myObject = new MyObject(5);
FileStream fs = new FileStream("test.xml", FileMode.OpenOrCreate);
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
serializer.Serialize(fs, myObject);
fs.Close();
}
}
[Serializable]
public class MyObject
{
public MyObject() {}
public MyObject(int state)
{
myState = state;
}
public int MyState
{
get
{
return myState;
}
set
{
myState = value;
}
}
private int myState;
}
"Joe Rizla" <te**@morphium.co.uk> wrote in message
news:OT********************@stones.force9.net...
Is there any example code I can find which shows how to Serialize an object to System.IO.Stream.

The reason why is because I want the pass the stream on: The Stream is then XSLT Transformed by passing the stream to create an XPathDocument object.
Thanks

rizlaroller

Nov 12 '05 #2
Thanks very much for this.
But am I mistaken or isn't this an example of Serialization to a file path
("test.xml"), and not to stream?

"Bennie Haelen" <Be***********@jda.com> wrote in message
news:Oh****************@tk2msftngp13.phx.gbl...
Here you go,
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyObject myObject = new MyObject(5);
FileStream fs = new FileStream("test.xml", FileMode.OpenOrCreate);
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
serializer.Serialize(fs, myObject);
fs.Close();
}
}
[Serializable]
public class MyObject
{
public MyObject() {}
public MyObject(int state)
{
myState = state;
}
public int MyState
{
get
{
return myState;
}
set
{
myState = value;
}
}
private int myState;
}
"Joe Rizla" <te**@morphium.co.uk> wrote in message
news:OT********************@stones.force9.net...
Is there any example code I can find which shows how to Serialize an

object
to System.IO.Stream.

The reason why is because I want the pass the stream on: The Stream is

then
XSLT Transformed by passing the stream to create an XPathDocument object.

Thanks

rizlaroller


Nov 12 '05 #3
sorry joe, send you the wrong file,

Here you go:

MyObject myObject = new MyObject(5);
System.IO.StreamWriter sw = new StreamWriter("test.xml");
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
serializer.Serialize(sw, myObject);
// access the stream
Stream stream = sw.BaseStream;
sw.Close();

You could also use a MemoryStream object if that works for you...

Regards,

Bennie Haelen
Bennie Haelen
"Joe Rizla" <te**@morphium.co.uk> wrote in message
news:WI*********************@wards.force9.net...
Thanks very much for this.
But am I mistaken or isn't this an example of Serialization to a file path
("test.xml"), and not to stream?

"Bennie Haelen" <Be***********@jda.com> wrote in message
news:Oh****************@tk2msftngp13.phx.gbl...
Here you go,
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyObject myObject = new MyObject(5);
FileStream fs = new FileStream("test.xml", FileMode.OpenOrCreate);
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
serializer.Serialize(fs, myObject);
fs.Close();
}
}
[Serializable]
public class MyObject
{
public MyObject() {}
public MyObject(int state)
{
myState = state;
}
public int MyState
{
get
{
return myState;
}
set
{
myState = value;
}
}
private int myState;
}
"Joe Rizla" <te**@morphium.co.uk> wrote in message
news:OT********************@stones.force9.net...
Is there any example code I can find which shows how to Serialize an

object
to System.IO.Stream.

The reason why is because I want the pass the stream on: The Stream is

then
XSLT Transformed by passing the stream to create an XPathDocument

object.

Thanks

rizlaroller



Nov 12 '05 #4
Thank you Bennie.

"Bennie Haelen" <Be***********@jda.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
sorry joe, send you the wrong file,

Here you go:

MyObject myObject = new MyObject(5);
System.IO.StreamWriter sw = new StreamWriter("test.xml");
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
serializer.Serialize(sw, myObject);
// access the stream
Stream stream = sw.BaseStream;
sw.Close();

You could also use a MemoryStream object if that works for you...

Regards,

Bennie Haelen
Bennie Haelen
"Joe Rizla" <te**@morphium.co.uk> wrote in message
news:WI*********************@wards.force9.net...
Thanks very much for this.
But am I mistaken or isn't this an example of Serialization to a file path
("test.xml"), and not to stream?

"Bennie Haelen" <Be***********@jda.com> wrote in message
news:Oh****************@tk2msftngp13.phx.gbl...
Here you go,
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyObject myObject = new MyObject(5);
FileStream fs = new FileStream("test.xml",

FileMode.OpenOrCreate); XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); serializer.Serialize(fs, myObject);
fs.Close();
}
}
[Serializable]
public class MyObject
{
public MyObject() {}
public MyObject(int state)
{
myState = state;
}
public int MyState
{
get
{
return myState;
}
set
{
myState = value;
}
}
private int myState;
}
"Joe Rizla" <te**@morphium.co.uk> wrote in message
news:OT********************@stones.force9.net...
> Is there any example code I can find which shows how to Serialize an
object
> to System.IO.Stream.
>
> The reason why is because I want the pass the stream on: The Stream is then
> XSLT Transformed by passing the stream to create an XPathDocument

object.
>
>
> Thanks
>
> rizlaroller
>
>



Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bjorn | last post by:
I know you should be careful before blaming internal frameworks, but this definitely does look like an internal bug. When serializing a ChildClass that inherits a BaseClass containing a struct...
2
by: reycri | last post by:
I have a .Net class (a collection) that already supports serialization. It implements ISerializable... Now, I need it to also support the COM interface IPersistStream. Among other things, I need...
4
by: ron | last post by:
Hi, I have class object that i serialize using the System.Xml.Serialization class. Intermittently the object is not getting serialized correctly, using System.Xml.Serialization classes....
1
by: Sacha | last post by:
I'm having trouble serializing some of my classes when using late binding. When I don't use late binding, it works fine. MyClass inherit from an abstract class called AbsMyClass. If I do the...
5
by: Tamir Khason | last post by:
I have an object (eg MyObject with namespace eg MyNamespace) public class MyObject While I serialize it the XML created is <MyObject xmlns:xsd="http://www.w3.org/2001/XMLSchema"...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
2
by: Maximus | last post by:
Hi Everyone, I was using Inprocess session objects, but incase of aspnet process crashes the session objects were lost as a result I decided to shift to out of porocess session objects. For this...
1
by: elziko | last post by:
My intention is to store an array of singles inside a DataTable so that it can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't know yet so I'm just saving it as an XML file for...
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
0
by: JosAH | last post by:
Introduction Upon hearing the word, "Serialization", the first question which comes to mind is ... "What is Serialization?" We know that we can create resusable objects in Java. But the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.