473,398 Members | 2,335 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,398 software developers and data experts.

xml serialization (interfaces)

Hi,

Can someone tell me how can I seralize the following PurchaseOder class
to xml.

public class PurchaseOrder
{
[XmlElement(typeof(USAddress))]
public IUSAddress shipTo;
}
public class USAddress:IUSAddress
{
private string _city;
public string city
{
get {return _city;}
set{_city = value;}
}
}
public interface IUSAddress
{
string city{get; set;}
}
I use the following code to serialize, but it crash on first stament
itself:

XmlSerializer serializer = new XmlSerializer(typeof(PurchaseOrder));
PurchaseOrder po = new PurchaseOrder();
po.shipTo = new USAddress();
po.shipTo.city = "my city";
TextWriter writer = new StreamWriter("PurchaseOrder2.xml");
serializer.Serialize(writer, po);
writer.Close();
Any help will me greatly appreciated.

Thanks,
Shrish

Dec 23 '05 #1
1 1691
Shrish,

The XmlElement attribute does not do what you think it does in this
context.

Unfortunately, XmlSerialization can not serialize properties that return
interfaces, as it doesn't know what type to use when deserializing it.

If you want to serialize this to XML, you would have to use the SOAP
formatter.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<sh********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hi,

Can someone tell me how can I seralize the following PurchaseOder class
to xml.

public class PurchaseOrder
{
[XmlElement(typeof(USAddress))]
public IUSAddress shipTo;
}
public class USAddress:IUSAddress
{
private string _city;
public string city
{
get {return _city;}
set{_city = value;}
}
}
public interface IUSAddress
{
string city{get; set;}
}
I use the following code to serialize, but it crash on first stament
itself:

XmlSerializer serializer = new XmlSerializer(typeof(PurchaseOrder));
PurchaseOrder po = new PurchaseOrder();
po.shipTo = new USAddress();
po.shipTo.city = "my city";
TextWriter writer = new StreamWriter("PurchaseOrder2.xml");
serializer.Serialize(writer, po);
writer.Close();
Any help will me greatly appreciated.

Thanks,
Shrish

Dec 23 '05 #2

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

Similar topics

2
by: Daniel Faensen | last post by:
As a good OO programmer that I hopefully am I prefer to implement against interfaces rather than classes. This is especially useful when it comes to multiple inheritance which is as you know an...
1
by: Daniel Lidström | last post by:
Hello, how can I control the order of which elements are serialized. For example, my class looks like: public __gc class Author { public: Author(); // get/set methods
0
by: Andy Searls via .NET 247 | last post by:
Trying to get business objects to work as return values for webservices. The primary problem is that the business objects arelate bound, requiring interfaces for property types. I tryusing the...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
0
by: Art | last post by:
Hi, I've got a lot of confusion about XML serialization. 2 specific questions: I'm using a book by Robin Reynolds-Haertle. In her section on serialization she starts with binary and...
4
by: Joe | last post by:
I would like to call a method of an object before the object is serialized. I currently have a surrogate that I use for deserilization and was thinking I could use one for serialization but that...
3
by: Zachary Turner | last post by:
Hello, I have a situation where I would like to perform custom serialization and deserialization of an existing .NET framework object (specifically, System.DateTime). Is there a common paradigm...
5
by: raj | last post by:
I know what interfaces are and what they used for etc. Today i am learning about serilization. I know to mark the class "serializable" and implement ISerializable interface, and then implement...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
0
by: JosAH | last post by:
Introduction Upon hearing the word, "Serialization", the first question which comes to mind is ... "What is Serialization?" We know that we can create resusable objects in Java. But the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.