1

当我尝试从 MyConfig.xml 反序列化时,出现内存不足异常

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

我的配置文件

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>

我不知道我做错了什么。请帮我解决这个问题。

谢谢!

4

1 回答 1

1

我已经通过在函数末尾编写reader.Read()来修复它......

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

    reader.Read();
}
于 2013-04-20T08:52:35.880 回答