473,569 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xml serialization (interfaces)

Hi,

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

public class PurchaseOrder
{
[XmlElement(type of(USAddress))]
public IUSAddress shipTo;
}
public class USAddress:IUSAd dress
{
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(t ypeof(PurchaseO rder));
PurchaseOrder po = new PurchaseOrder() ;
po.shipTo = new USAddress();
po.shipTo.city = "my city";
TextWriter writer = new StreamWriter("P urchaseOrder2.x ml");
serializer.Seri alize(writer, po);
writer.Close();
Any help will me greatly appreciated.

Thanks,
Shrish

Dec 23 '05 #1
1 1698
Shrish,

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

Unfortunately, XmlSerializatio n 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.co m

<sh********@gma il.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
Hi,

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

public class PurchaseOrder
{
[XmlElement(type of(USAddress))]
public IUSAddress shipTo;
}
public class USAddress:IUSAd dress
{
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(t ypeof(PurchaseO rder));
PurchaseOrder po = new PurchaseOrder() ;
po.shipTo = new USAddress();
po.shipTo.city = "my city";
TextWriter writer = new StreamWriter("P urchaseOrder2.x ml");
serializer.Seri alize(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
10753
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 issue and not supported in languages as Java or C# - for good reasons. I have an object graph I want to persist using the XML serialization features...
1
1660
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
1345
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 XmlTypeAttribute to indicate the class to use in placeof the interface, but that doesn't seem to do any good. Here's the simplified objects in...
6
2707
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 rmi. Just to keep my question short I am posting trimmed version of my code. //file: Serializable.cs
0
282
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 follows it up with XML. I did that. Then I notice, I think, that for the XML part I don't need the <serializable> attribute, the ISerializable interface...
4
1879
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 means I have to handle GetObjectData for each object which I don't want to do unless there is a way to make a generic one to work with any object. ...
3
2631
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 here, for how I can override the default behavior of DateTime serialization and provide my own? I want to use this custom serialized DateTime in...
5
1359
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 mthod GetObjectData, etc. I also know to instantiate a class that is marked "serializable" and use serialize method of binaryformatter on the...
4
11392
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 look at innerException it says: "Cannot serialize member 'System.ComponentModel.Component.Site' of type 'System.ComponentModel.ISite'. OK it is...
0
4701
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 lifetime of those objects lasts only as long as the Java virtual machine is running. Once we close the JVM, we lose all those objects. What if someone...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.