BigInteger serialization

Is there any method to serialize BigInteger to and from an XML file?

The following is a short snippet demonstrating how I'm currently serializing classes:

static public void SerializeToXML( Report report )
{
    XmlSerializer serializer = new XmlSerializer( typeof( Report ) );
    using ( TextWriter textWriter = new StreamWriter( Path.Combine( report.Path, report.Filename ) ) )
    {
        serializer.Serialize( textWriter, report );
    }
}

[Serializable]
public class Report
{
    public BigInteger CurrentMaximum { get; set; }
}

All other properties of the Report class are correctly serialized, but BigInteger properties do not. Is there any way to serialize this property?

+5
source share
1 answer

Unfortunately, it is XmlSerializerintended to create XML, described using a standard XML schema; which were part of the .Net 1.0 phase (which means xs: integer is not supported).

IXmlSerializable

BigInteger IXmlSerializable. XSD round-tripping (, WebService), .

public class BigInteger : IXmlSerializable
{
    public int Value;

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        // You should really a create schema for this.
        // Hardcoded is fine.
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        Value = int.Parse(reader.ReadString());
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteValue(Value.ToString());
    }
}

- ( , , )

( , xs: string) intellisense.

[XmlRoot("foo")]
public class SerializePlease
{
    [XmlIgnore]
    public BigInteger BigIntValue;

    [XmlElement("BigIntValue")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public string BigIntValueProxy
    {
        get
        {
            return BigIntValue.ToString();
        }
        set
        {
            BigIntValue = BigInteger.Parse(value);
        }
    }
}
+4

All Articles