序列化 BigInteger

发布于 2024-11-24 20:48:59 字数 573 浏览 1 评论 0原文

是否有任何方法可以将 BigInteger 序列化到 XML 文件或从 XML 文件序列化 BigInteger?

下面是一个简短的片段,演示了我当前如何序列化类:

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

Report 类中的所有其他属性都已正确序列化,但是 BigInteger 属性却没有。有没有办法序列化这个属性?

Is there any method of serializing a BigInteger to and from an XML file?

Below is a short snippet that demonstrates 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 within the Report class get serialized correctly, however, the BigInteger properties do not. Is there a way to serialize this property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

禾厶谷欠 2024-12-01 20:48:59

不幸的是,XmlSerializer 旨在创建可使用标准 XML 架构描述的 XML; .Net 1.0 阶段框架中的内容(这意味着 xs:integer)。

IXmlSerialized

您需要修改 BigInteger 类并向其添加 IXmlSerialized 接口。如果您需要使用 XSD 往返(例如 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)创建一个新属性并隐藏来自智能感知。

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

Unfortunately XmlSerializer is designed to create XML describeable using a standard XML Schema; that were in the framework during the .Net 1.0 phase (which means xs:integer isn't supported).

IXmlSerializable

You will need to modify the BigInteger class and add the IXmlSerializable interface to it. If you need to use XSD round-tripping (e.g. WebService references) this might cause problems.

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

Proxy Property (Although this looks like it has code-smell it is way more portable)

Create a new property with a supported schema type (most probably xs:string) and hide it from 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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文