Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 11th, 2005, 10:51 PM
Guest
 
Posts: n/a
Default Xml Serialize override problem

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();
}
}

}

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles