471,347 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,347 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 1644
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Daniel Faensen | last post: by
1 post views Thread by Daniel Lidström | last post: by
reply views Thread by Andy Searls via .NET 247 | last post: by
6 posts views Thread by Uttam | last post: by
reply views Thread by Art | last post: by
4 posts views Thread by Joe | last post: by
3 posts views Thread by Zachary Turner | last post: by
4 posts views Thread by mijalko | last post: by
reply views Thread by JosAH | last post: by

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.