Serialize IPAddress

G

Guest

How might I serialize an IPAddress?

I read it needs to have a default constructor so I made a wrapper class:
[Serializable]
public class IPAddressEx : IPAddress
{
public IPAddressEx()
: base( IPAddress.None.GetAddressBytes() )
{

}

public IPAddressEx( byte[] address )
: base( address )
{

}

public static IPAddressEx Convert( IPAddress address )
{
IPAddressEx toReturn = new IPAddressEx(
address.GetAddressBytes() );

return toReturn;
}
}

But I still get an error when I serialize this IPAddressEx:
There was an error generating the XML document.

When I execute this code to serialize:
XmlSerializer serializer = new XmlSerializer( typeof(
Printers.IPAddressEx ) );
XmlWriter output = XmlWriter.Create( configPath );

serializer.Serialize( output, new Printers.IPAddressEx() );

Thanks,
Chris
 
W

Wei-Dong XU [MSFT]

Hi,

Currently I am finding one support professional for you regarding this
issue. If any update, we will post back at the first time.

Please feel free to let me know if you have any further question on this
matter.

Best Regards,
Wei-Dong XU
Microsoft Product Support Services
This posting is provided "AS IS" with no warranties, and confers no rights.
It is my pleasure to be of assistance.
 
P

Peter Huang [MSFT]

Hi

To serialize XML, we need to declare some xml attribute.

Here are some links for your reference.

Troubleshooting Common Problems with the XmlSerializer
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/ht
ml/trblshtxsd.asp

PRB: XML Serialization: System.Xml.XmlSerializer Does Not Serialize Default
Values

Examples of XML Serialization
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconAnExampleOfXMLSerializationWithXmlSerializer.asp

Introducing XML Serialization
http://winfx.msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_fx
serialization/html/8c63200d-db63-4a03-a93d-21641623df62.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top