Program.... XML serialization

A

Arjen

Hello,

Can somebody help me a little bit?

I can't get it to work.
Please see my code below... I have placed some comments like "// And whats
next?".
I'm sure that I have to code something here. But what?

What do I want?
- Serialize and deserialize ObjectsA and ObjectsB.

Thanks!
Arjen




namespace ProgramMainName {

class ProgramMainName {

public static Hashtable ObjectsA = new Hashtable();
public static Hashtable ObjectsB = new Hashtable();

/// <summary>
/// The main!
/// </summary>
[STAThread]
static void Main(string[] args) {
// To do something, like: XMLSerialization.Open()
}
}
}





namespace ProgramMainName {
public class XMLSerialization {

public static void Open() {

XmlSerializer x = new XmlSerializer( typeof( ProgramMainName ) );

FileStream fs = null;
try {
fs = new FileStream( "data.xml", FileMode.Open );

XmlReader reader = new XmlTextReader( fs );
ProgramMainName programMainName = (ProgramMainName) x.Deserialize(
reader );
// And whats next?
}
catch( FileNotFoundException ) {

}
finally {
if ( fs != null )
fs.Close();
}
}
}

public static void Save() {
// And whats next?
}
}
 
C

Cezary Nolewajka

A good idea is to use a serializer, either a binary serializer or soap serilizer.
All objects that you add to the Hashtable must be marked as serilizable:

[Serializable]
class TestClass
{
public int iVal = 8;
public string strVal = "member value";
}

Then, you can perform serialization and deserialization:

Hashtable objA = new Hashtable();
Hashtable objB = new Hashtable();

System.Runtime.Serialization.Formatters.Soap.SoapFormatter x = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter ();

TestClass tc = new TestClass ();
tc.iVal = 10;

objA.Add ("key1", "value key1");
objA.Add ("key2", "value key2");
objA.Add ("key3", tc);

FileStream fs = new FileStream ("Store.xml",FileMode.OpenOrCreate ,FileAccess.Write );
x.Serialize (fs, objA);
fs.Close ();

fs = new FileStream ("Store.xml", FileMode.Open, FileAccess.Read);
objB = (Hashtable) x.Deserialize (fs);

IDictionaryEnumerator en = objB.GetEnumerator ();
while (en.MoveNext ())
{
Debug.WriteLine (string.Format ("{0}: {1}", en.Key, objB [en.Key]));
}
Debug.WriteLine ("val3: " + "iVal: " + ((TestClass)objB ["key3"]).iVal);

There is a difference between the XML and binary serialization. An exert from tne .net documantation:

Note XML serialization does not convert methods, indexers, private fields, or read-only properties (except read-only collections). To serialize all of an object's fields and properties, both public and private, use the BinaryFormatter instead of XML serialization.

I tried to use XmlSerializer but didn't manage. According to the documentation a collection that implements ICollection or IEnumerable (which both are implemented by Hashtable) should be serializable with XmlSerializer. But while trying to use XmlSerializer with the typeof (Hashtable) I kept on getting the error:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll
Additional information: There was an error reflecting 'System.Collections.Hashtable'.
Arjen said:
Hello,

Can somebody help me a little bit?

I can't get it to work.
Please see my code below... I have placed some comments like "// And whats
next?".
I'm sure that I have to code something here. But what?

What do I want?
- Serialize and deserialize ObjectsA and ObjectsB.

Thanks!
Arjen




namespace ProgramMainName {

class ProgramMainName {

public static Hashtable ObjectsA = new Hashtable();
public static Hashtable ObjectsB = new Hashtable();

/// <summary>
/// The main!
/// </summary>
[STAThread]
static void Main(string[] args) {
// To do something, like: XMLSerialization.Open()
}
}
}





namespace ProgramMainName {
public class XMLSerialization {

public static void Open() {

XmlSerializer x = new XmlSerializer( typeof( ProgramMainName ) );

FileStream fs = null;
try {
fs = new FileStream( "data.xml", FileMode.Open );

XmlReader reader = new XmlTextReader( fs );
ProgramMainName programMainName = (ProgramMainName) x.Deserialize(
reader );
// And whats next?
}
catch( FileNotFoundException ) {

}
finally {
if ( fs != null )
fs.Close();
}
}
}

public static void Save() {
// And whats next?
}
}
 
A

Arjen

Cezary Nolewajka,

Thanks for your answer.

I see that you are putting data from objA to objB.
My questions was more how to serialize them both in the same file.
If you do that then you have no root element.

The solotion will be to serialize the class... if tried it but can't get it
to work.

Arjen


"Cezary Nolewajka" <[email protected]> schreef in
bericht A good idea is to use a serializer, either a binary serializer or soap
serilizer.
All objects that you add to the Hashtable must be marked as serilizable:

[Serializable]
class TestClass
{
public int iVal = 8;
public string strVal = "member value";
}

Then, you can perform serialization and deserialization:

Hashtable objA = new Hashtable();
Hashtable objB = new Hashtable();

System.Runtime.Serialization.Formatters.Soap.SoapFormatter x = new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter ();

TestClass tc = new TestClass ();
tc.iVal = 10;

objA.Add ("key1", "value key1");
objA.Add ("key2", "value key2");
objA.Add ("key3", tc);

FileStream fs = new FileStream ("Store.xml",FileMode.OpenOrCreate
,FileAccess.Write );
x.Serialize (fs, objA);
fs.Close ();

fs = new FileStream ("Store.xml", FileMode.Open, FileAccess.Read);
objB = (Hashtable) x.Deserialize (fs);

IDictionaryEnumerator en = objB.GetEnumerator ();
while (en.MoveNext ())
{
Debug.WriteLine (string.Format ("{0}: {1}", en.Key, objB [en.Key]));
}
Debug.WriteLine ("val3: " + "iVal: " + ((TestClass)objB ["key3"]).iVal);

There is a difference between the XML and binary serialization. An exert
from tne .net documantation:

Note XML serialization does not convert methods, indexers, private
fields, or read-only properties (except read-only collections). To serialize
all of an object's fields and properties, both public and private, use the
BinaryFormatter instead of XML serialization.

I tried to use XmlSerializer but didn't manage. According to the
documentation a collection that implements ICollection or IEnumerable (which
both are implemented by Hashtable) should be serializable with
XmlSerializer. But while trying to use XmlSerializer with the typeof
(Hashtable) I kept on getting the error:

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.xml.dll
Additional information: There was an error reflecting
'System.Collections.Hashtable'.
Arjen said:
Hello,

Can somebody help me a little bit?

I can't get it to work.
Please see my code below... I have placed some comments like "// And whats
next?".
I'm sure that I have to code something here. But what?

What do I want?
- Serialize and deserialize ObjectsA and ObjectsB.

Thanks!
Arjen




namespace ProgramMainName {

class ProgramMainName {

public static Hashtable ObjectsA = new Hashtable();
public static Hashtable ObjectsB = new Hashtable();

/// <summary>
/// The main!
/// </summary>
[STAThread]
static void Main(string[] args) {
// To do something, like: XMLSerialization.Open()
}
}
}





namespace ProgramMainName {
public class XMLSerialization {

public static void Open() {

XmlSerializer x = new XmlSerializer( typeof( ProgramMainName ) );

FileStream fs = null;
try {
fs = new FileStream( "data.xml", FileMode.Open );

XmlReader reader = new XmlTextReader( fs );
ProgramMainName programMainName = (ProgramMainName) x.Deserialize(
reader );
// And whats next?
}
catch( FileNotFoundException ) {

}
finally {
if ( fs != null )
fs.Close();
}
}
}

public static void Save() {
// And whats next?
}
}
 
C

Cezary Nolewajka

Well, just put both Hashtables into one class, make it serializable and here you go:

[Serializable]
public class TestClass
{
public int iVal = 8;
public string strVal = "member value";
}

[Serializable]
public class ContainerClass
{
public Hashtable objA = new Hashtable();
public Hashtable objB = new Hashtable();
}


System.Runtime.Serialization.Formatters.Soap.SoapFormatter x = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter ();

ContainerClass obj = new ContainerClass ();

TestClass tc = new TestClass ();
tc.iVal = 10;

obj.objA.Add ("key1", "value key1");
obj.objA.Add ("key2", "value key2");
obj.objA.Add ("key3", tc);

obj.objB.Add ("key4", "value key4");
obj.objB.Add ("key5", "value key5");
obj.objB.Add ("key6", tc);

FileStream fs = new FileStream ("Store.xml",FileMode.OpenOrCreate ,FileAccess.Write );
x.Serialize (fs, obj);
fs.Close ();

fs = new FileStream ("Store.xml", FileMode.Open, FileAccess.Read);
obj = (ContainerClass) x.Deserialize (fs);

IDictionaryEnumerator en = obj.objA.GetEnumerator ();
while (en.MoveNext ())
{
Debug.WriteLine (string.Format ("{0}: {1}", en.Key, obj.objA [en.Key]));
}
Debug.WriteLine ("val3: " + "iVal: " + ((TestClass)obj.objA ["key3"]).iVal);

en = obj.objB.GetEnumerator ();
while (en.MoveNext ())
{
Debug.WriteLine (string.Format ("{0}: {1}", en.Key, obj.objB [en.Key]));
}
Debug.WriteLine ("val6: " + "iVal: " + ((TestClass)obj.objB ["key6"]).iVal);


Arjen said:
Cezary Nolewajka,

Thanks for your answer.

I see that you are putting data from objA to objB.
My questions was more how to serialize them both in the same file.
If you do that then you have no root element.

The solotion will be to serialize the class... if tried it but can't get it
to work.

Arjen


"Cezary Nolewajka" <[email protected]> schreef in
bericht A good idea is to use a serializer, either a binary serializer or soap
serilizer.
All objects that you add to the Hashtable must be marked as serilizable:

[Serializable]
class TestClass
{
public int iVal = 8;
public string strVal = "member value";
}

Then, you can perform serialization and deserialization:

Hashtable objA = new Hashtable();
Hashtable objB = new Hashtable();

System.Runtime.Serialization.Formatters.Soap.SoapFormatter x = new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter ();

TestClass tc = new TestClass ();
tc.iVal = 10;

objA.Add ("key1", "value key1");
objA.Add ("key2", "value key2");
objA.Add ("key3", tc);

FileStream fs = new FileStream ("Store.xml",FileMode.OpenOrCreate
,FileAccess.Write );
x.Serialize (fs, objA);
fs.Close ();

fs = new FileStream ("Store.xml", FileMode.Open, FileAccess.Read);
objB = (Hashtable) x.Deserialize (fs);

IDictionaryEnumerator en = objB.GetEnumerator ();
while (en.MoveNext ())
{
Debug.WriteLine (string.Format ("{0}: {1}", en.Key, objB [en.Key]));
}
Debug.WriteLine ("val3: " + "iVal: " + ((TestClass)objB ["key3"]).iVal);

There is a difference between the XML and binary serialization. An exert
from tne .net documantation:

Note XML serialization does not convert methods, indexers, private
fields, or read-only properties (except read-only collections). To serialize
all of an object's fields and properties, both public and private, use the
BinaryFormatter instead of XML serialization.

I tried to use XmlSerializer but didn't manage. According to the
documentation a collection that implements ICollection or IEnumerable (which
both are implemented by Hashtable) should be serializable with
XmlSerializer. But while trying to use XmlSerializer with the typeof
(Hashtable) I kept on getting the error:

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.xml.dll
Additional information: There was an error reflecting
'System.Collections.Hashtable'.
Arjen said:
Hello,

Can somebody help me a little bit?

I can't get it to work.
Please see my code below... I have placed some comments like "// And whats
next?".
I'm sure that I have to code something here. But what?

What do I want?
- Serialize and deserialize ObjectsA and ObjectsB.

Thanks!
Arjen




namespace ProgramMainName {

class ProgramMainName {

public static Hashtable ObjectsA = new Hashtable();
public static Hashtable ObjectsB = new Hashtable();

/// <summary>
/// The main!
/// </summary>
[STAThread]
static void Main(string[] args) {
// To do something, like: XMLSerialization.Open()
}
}
}





namespace ProgramMainName {
public class XMLSerialization {

public static void Open() {

XmlSerializer x = new XmlSerializer( typeof( ProgramMainName ) );

FileStream fs = null;
try {
fs = new FileStream( "data.xml", FileMode.Open );

XmlReader reader = new XmlTextReader( fs );
ProgramMainName programMainName = (ProgramMainName) x.Deserialize(
reader );
// And whats next?
}
catch( FileNotFoundException ) {

}
finally {
if ( fs != null )
fs.Close();
}
}
}

public static void Save() {
// And whats next?
}
}
 
A

Arjen

Hi there,

I have tried to run your sample without succes.
So I have made a new sample. Maybe you can fix the serialization?

The class "MyProgram" must be saved to an xml file and placed back in the
program with the "open" call.

Hope you can do something for me.
Thanks,
Arjen


// -------------------------------------------------------------------------
-----------


using System;
using System.Collections;

namespace MyProgram {

class MyProgram {

public static Hashtable Persons = new Hashtable();

[STAThread]
static void Main(string[] args) {
Open();

DoSomeThing();

Save();
}

static public void DoSomeThing() {

// New object
Person person = new Person("James", 12);

// Add to the hashtable
Persons.Add( person.GetHashCode(), person);
}


static void Open() {
// Read the serialization

}

static void Save() {
// Save the serialization

}
}

class Person {
string _name;
int _age;

public Person( string name, int age) {
_name = name;
_age = age;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top