T
Tony Johansson
Hello!
I try to understand why I can't XML serialize when I use inheriatance with
abstract class.
In this example I have a Person class which is abstract and the base class.
I have then a derived class Student as you can see here.
public abstract class Person
{
private string name;
public Person() //Default c_tor
{}
public Person(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
}
public class Student : Person
{
private string collage;
public Student() //Default C-tor
{}
public Student(string name, string collage) : base(name)
{
this.collage = collage;
}
public string Collage
{
get { return collage; }
set { collage = value; }
}
}
I have then an event hander called MenuFileExportToXML that is listed just
below. The code maked 1 and 2 below give an Exception in the generic method
XMLSerialize that is listed below the exception says.It was not possible to
generate the XML-documentet. As you can see in the code marked 1 and 2 I
create the generic List as List<Person>
In the code marked 3 below I it works fine because here I create the generic
List as List<Student>.
So my question is how should I write to be able to serialize the object
Person ?
This seems very strange because writing as I have done in the code marked as
1 and 2 is very common but the
XmlSerializer doesn't like it for some reason.
private void MenuFileExportToXML_Click(object sender, EventArgs e)
{
1 List<Person> myListNotWork1 = new List<Person>();
1 Person p1 = new Student("olle", "sundsta");
1 myListNotWork1.Add(p1);
1 XMLSerialization.XMLSerialize<List<Person>>(XMLFile, myListNotWork1);
2 List<Person> myListNotWork2= new List<Person>();
2 Student s1 = new Student("olle", "sundsta");
2 myListNotWork2.Add(s1);
2 XMLSerialization.XMLSerialize<List<Person>>(XMLFile, myListNotWork2);
3 List<Student> myListWork = new List<Student>();
3 Student s1 = new Student("olle", "sundsta");
3 myListWork.Add(s1);
3 XMLSerialization.XMLSerialize<List<Student>>(XMLFile, myListWork);
}
public class XMLSerialization
{
public static void XMLSerialize<T>(string filePath, T obj)
{
try
{
using (FileStream fs = new FileStream(filePath,
FileMode.Create))
{
XmlSerializer XmlSer = new XmlSerializer(typeof(T));
XmlSer.Serialize(fs, obj);
}
}
catch(Exception ex)
{
string slask = ex.Message;
}
}
}
//Tony
I try to understand why I can't XML serialize when I use inheriatance with
abstract class.
In this example I have a Person class which is abstract and the base class.
I have then a derived class Student as you can see here.
public abstract class Person
{
private string name;
public Person() //Default c_tor
{}
public Person(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
}
public class Student : Person
{
private string collage;
public Student() //Default C-tor
{}
public Student(string name, string collage) : base(name)
{
this.collage = collage;
}
public string Collage
{
get { return collage; }
set { collage = value; }
}
}
I have then an event hander called MenuFileExportToXML that is listed just
below. The code maked 1 and 2 below give an Exception in the generic method
XMLSerialize that is listed below the exception says.It was not possible to
generate the XML-documentet. As you can see in the code marked 1 and 2 I
create the generic List as List<Person>
In the code marked 3 below I it works fine because here I create the generic
List as List<Student>.
So my question is how should I write to be able to serialize the object
Person ?
This seems very strange because writing as I have done in the code marked as
1 and 2 is very common but the
XmlSerializer doesn't like it for some reason.
private void MenuFileExportToXML_Click(object sender, EventArgs e)
{
1 List<Person> myListNotWork1 = new List<Person>();
1 Person p1 = new Student("olle", "sundsta");
1 myListNotWork1.Add(p1);
1 XMLSerialization.XMLSerialize<List<Person>>(XMLFile, myListNotWork1);
2 List<Person> myListNotWork2= new List<Person>();
2 Student s1 = new Student("olle", "sundsta");
2 myListNotWork2.Add(s1);
2 XMLSerialization.XMLSerialize<List<Person>>(XMLFile, myListNotWork2);
3 List<Student> myListWork = new List<Student>();
3 Student s1 = new Student("olle", "sundsta");
3 myListWork.Add(s1);
3 XMLSerialization.XMLSerialize<List<Student>>(XMLFile, myListWork);
}
public class XMLSerialization
{
public static void XMLSerialize<T>(string filePath, T obj)
{
try
{
using (FileStream fs = new FileStream(filePath,
FileMode.Create))
{
XmlSerializer XmlSer = new XmlSerializer(typeof(T));
XmlSer.Serialize(fs, obj);
}
}
catch(Exception ex)
{
string slask = ex.Message;
}
}
}
//Tony