IPAddress and XmlSerializer

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() );


"There was an error generating the XML document."

Inner exception: "The attempted operation is not supported for the type of
object referenced"
 
O

Ollie Riches

Hi,

You can add support for xml serialisation to a class by implementing the
IXmlSerializable interface, this means providing implementations of the
following methods:

public System.Xml.Schema.XmlSchema GetSchema()
public void ReadXml(System.Xml.XmlReader reader)
public void WriteXml(System.Xml.XmlWriter writer)

For an example on class that is not IPAddress check out:

http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx

HTH

Ollie Riches
 

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