Hello,
I have made a little test application and needs some help.
I have two classes with hash tables which I want to serialize inside one xml
file.
My question is if someone can make the code complete? (loading and saving)
- I have made some starting points.
- The // comment tags says where to do "something".
- What the "something" means I don't know... but I have tried...
I have copy paste the code of this console application under this message.
If you have any questions please ask me! Because I want to make a working
testapplication.
With some help it must be no problem! (I hope so)
Many thanks!
Arjen
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace TestApplication
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class TestApplication
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// LoadXML
loadXML();
// Add some persons and animals
// SaveXML
saveXML();
}
static void loadXML() {
XmlSerializer x = new XmlSerializer( typeof(Container[]) );
FileStream fs = null;
try {
fs = new FileStream( "data.xml", FileMode.Open );
XmlReader reader = new XmlTextReader( fs );
Container[] container = (Container[]) x.Deserialize( reader );
//
}
catch( FileNotFoundException ) {
}
finally {
if ( fs != null ) fs.Close();
}
}
static void saveXML() {
//
}
}
public class Container {
public Container() {
// I want to load/save the Hashtables of person and animals.
// inside one xml file
// Person persons = new Person();
// Animal animals = new Animal();
}
}
public class Person {
private Hashtable myTable = new Hashtable();
private string _name;
public string Name {
get {
return _name;
}
set {
_name = value;
}
}
public void AddPeron ( Person person ) {
try {
myTable.Add( person.GetHashCode(), person );
}
catch( ArgumentException ) {
}
}
public Person() {
}
}
public class Animal {
private Hashtable myTable = new Hashtable();
private string _name;
public string Name {
get {
return _name;
}
set {
_name = value;
}
}
public void AddAnimal ( Animal animal ) {
try {
myTable.Add( animal.GetHashCode(), animal );
}
catch( ArgumentException ) {
}
}
public Animal() {
}
}
}