473,503 Members | 1,136 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(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 1695
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
10748
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
1656
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
1340
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
2701
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
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...
4
1876
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
2621
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
1358
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
11379
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
4697
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
7089
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
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
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
7463
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
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
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...

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.