The code is below.. I want to be able to override two
classes to the same element name... I've commented out
the code which I cant get to work... the base of the code
is an extention of one of the .Net examples...
using System.Xml;
using System.Xml.Serialization;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System;
namespace ConsoleApplication1
{
[System.Xml.Serialization.XmlTypeAttribute("library ")]
[System.Xml.Serialization.XmlRootAttribute
(IsNullable=false)]
public class library
{
[System.Xml.Serialization.XmlElementAttribute
(Type=typeof(Order))]
public ArrayList Orders;
public class Order
{
[System.Xml.Serialization.XmlElementAttribute
("Books",Type=typeof(Book))]
[System.Xml.Serialization.XmlElementAttribute
(Type=typeof(ExpandedBook))]
//[System.Xml.Serialization.XmlElementAttribute
(Type=typeof(ExpandedBook2))]
public ArrayList Books;
}
public class Book
{
[System.Xml.Serialization.XmlElementAttribute()]
public string ISBN;
}
public class ExpandedBook:Book
{
[System.Xml.Serialization.XmlElementAttribute()]
public bool NewEdition;
}
public class ExpandedBook2:Book
{
}
}
public class Run
{
static void Main(string[] args)
{
Run ser = new Run();
ser.SerializeObject("c:\\class.xml");
}
public void SerializeObject(string filename)
{
XmlAttributes attrs = new XmlAttributes();
XmlElementAttribute attra = new XmlElementAttribute();
attra.ElementName = "BookImNew";
attra.Type = typeof(library.ExpandedBook);
attrs.XmlElements.Add(attra);
//THIS IS THE MAIN PROBLEM
//Specify the same element name for a derived class
//XmlElementAttribute attrb = new XmlElementAttribute
();
//attrb.ElementName = "BookImNew";
//attrb.Type = typeof(library.ExpandedBook2);
//attrs.XmlElements.Add(attrb);
XmlAttributeOverrides attrOverrides = new
XmlAttributeOverrides();
attrOverrides.Add(typeof(library.Order), "Books",
attrs);
XmlSerializer s =
new XmlSerializer(typeof(library), attrOverrides);
TextWriter writer = new StreamWriter(filename);
library newlib = new library();
// Creates the object that will be serialized.
library.Order myOrders = new library.Order();
myOrders.Books = new ArrayList ( ) ;
// Creates an object of the derived type.
library.ExpandedBook a = new library.ExpandedBook();
a.ISBN= "123456789";
a.NewEdition = true;
myOrders.Books.Add(a);
// Creates an object of the derived type.
//library.ExpandedBook2 b = new library.ExpandedBook2
();
//b.ISBN= "123456789";
//myOrders.Books.Add(b);
newlib.Orders = new ArrayList ( ) ;
newlib.Orders.Add(myOrders);
// Serializes the object.
s.Serialize(writer,newlib);
writer.Close();
}
}
}