473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

}

Nov 11 '05 #1
0 2731

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: grwalker | last post by:
I have some classes that have the <Serializable()> attribute applied, which of course by default serializes the class properties as elements. What I would like to do is to be able to override this...
7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
6
by: Peter Stojkovic | last post by:
I am using a xlmserializer , to write into a file . serializer.Serialize(writer, po, ns) Everything works fine. But instead of writing into a file, i want serialize into a string. How can I...
3
by: matt.skibbs | last post by:
When we moved a project from .NET 1.1 to .NET 2.0, we ran into an issue with some code that inserts a datatable from IDataReader.GetSchemaTable() into a dataset, and then serializes the dataset to...
9
by: Gillard | last post by:
i get an exeption and i do not know what else to do to continue Dim sdf As New SaveFileDialog With sdf .AddExtension = True .DefaultExt = ".record" .FileName = Now.ToLongDateString .Filter =...
4
by: djidan | last post by:
hi, i am a newbe to c#, i'm trying to send en custom object from a client to a server, after reading a lot on the web ifound that i need to use: BinaryFormatter, MemoryStream, and Serialize...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.