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" <c.************ *********@no-sp-am-eh-mail.com> schreef in
bericht news:uR******** ******@TK2MSFTN GP09.phx.gbl...
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.F ormatters.Soap. SoapFormatter x = new
System.Runtime. Serialization.F ormatters.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",Fi leMode.OpenOrCr eate
,FileAccess.Wri te );
x.Serialize (fs, objA);
fs.Close ();
fs = new FileStream ("Store.xml" , FileMode.Open, FileAccess.Read );
objB = (Hashtable) x.Deserialize (fs);
IDictionaryEnum erator en = objB.GetEnumera tor ();
while (en.MoveNext ())
{
Debug.WriteLine (string.Format ("{0}: {1}", en.Key, objB [en.Key]));
}
Debug.WriteLine ("val3: " + "iVal: " + ((TestClass)obj B ["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.Invalid OperationExcept ion' occurred
in system.xml.dll
Additional information: There was an error reflecting
'System.Collect ions.Hashtable' .
"Arjen" <bo*****@hotmai l.com> wrote in message
news:bv******** **@news2.tilbu1 .nb.home.nl...
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: XMLSerializatio n.Open()
}
}
}
namespace ProgramMainName {
public class XMLSerializatio n {
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 = (ProgramMainNam e) x.Deserialize(
reader );
// And whats next?
}
catch( FileNotFoundExc eption ) {
}
finally {
if ( fs != null )
fs.Close();
}
}
}
public static void Save() {
// And whats next?
}
}