chris1302
chris1302

Reputation: 21

Out of memory exception on deserializing IPAddress

When I try to deserialize from MyConfig.xml I get an out of memory exception at

System.Net.IPAddress.InternalParse(String ipString, Boolean tryParse)
System.Net.IPAddress.Parse(String ipString)
MyNamespace.IPRange.ReadXml(XmlReader reader)

IPRange.cs

public class IPRange : IXmlSerializable
{
    public IPRange () { }

    public IPAddress StartIP { get; set; }
    public IPAddress EndIP { get; set; }

    public XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

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

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

MyConfig.cs

public class MyConfig
{
    [XmlArrayItem("IPRange")]
    public List<IPRange> DMZ { get; set; }
}

MyConfig.xml

<?xml version="1.0" encoding="utf-8" ?>
<MyConfig>
    <DMZ>
        <IPRange StartIP="{some start ip}" EndIP="{some end ip}" />
        <IPRange StartIP="{some start ip}" EndIP="{some end ip}" />
    </DMZ>
</MyConfig>

I don't know what I'm doing wrong. Please help me with this problem.

Thanks!

Upvotes: 1

Views: 271

Answers (1)

chris1302
chris1302

Reputation: 21

I've fixed it by writing reader.Read() at the end of the function...

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

    reader.Read();
}

Upvotes: 1

Related Questions