Working on a project today, i needed to serialize one of my classes that included at System.Net.IPAddress property.

Problem problem is that IPAddress does not support XML Serialization since it does not implement a parameterless constructor!

I tried to search the net, but couldnt find a complete guide, therefore i am posting this, hoping to help others in the same situation!

My solution is to serialize the IPAddresses as a string, and later parse them top ip adresses again.

public class IPRange : IComparable, IXmlSerializable
    {
        public IPAddress StartIP { get; set; }
        public IPAddress EndIP { get; set; }

        public IPRange()
        {
        }

        public IPRange(IPAddress start, IPAddress end)
        {
            this.StartIP = start;
            this.EndIP = end;
        }


        public double StartDec
        {
            get
            {
                return Dot2Dec(this.StartIP);
            }
        }

        public double EndDec
        {
            get
            {
                return Dot2Dec(this.EndIP);
            }
        }

        #region Private Helpers
        
        private double Dot2Dec(IPAddress ip)
            {
                string ipstring = ip.ToString();
                var arrIP = ipstring.Split('.');
                return (Convert.ToInt64(arrIP[0]) * (Math.Pow(2, 24))) + (Convert.ToInt64(arrIP[1]) * (Math.Pow(2, 16))) + (Convert.ToInt64(arrIP[2]) * (Math.Pow(2, 8))) + Convert.ToInt64(arrIP[3]);
            }

        #endregion

        #region IComparable<Boundary> Members

        public int CompareTo(Boundary other)
        {
            return this.StartDec.CompareTo(other.IPRange.StartDec);
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            double thisValue = this.StartDec;
            double otherValue = (obj as IPRange).StartDec;

            return thisValue.CompareTo(otherValue);
        }

        #endregion

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            this.StartIP = IPAddress.Parse(reader.GetAttribute("StartIP"));
            this.EndIP = IPAddress.Parse(reader.GetAttribute("EndIP"));
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("StartIP", StartIP.ToString());
            writer.WriteAttributeString("EndIP", EndIP.ToString());

        }

        #endregion
    }

 

As you can see above i have used the IXmlSerialization interface, to define my custom serialization and deserialization of the IPAddress objects.

I have included my whole class where i have included 2 properties “StartDec” and “EndDec”

These are a double, containing the converter ip adresses. I use them for checking if an ip is inside the defined range. and to make them sortable in list etc.

Thats all for today. Have fun!