By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,162 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,162 IT Pros & Developers. It's quick & easy.

IPAddress and XmlSerializer

P: n/a
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"
Jan 31 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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/arc...03/444961.aspx

HTH

Ollie Riches

"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:FA**********************************@microsof t.com...
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"

Jan 31 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.