使用 XmlSerializer 序列化简单数组

发布于 2024-11-27 09:27:48 字数 1043 浏览 0 评论 0原文

已经晚了而且完全有可能我错过了一些明显的东西,但它是什么?

我正在尝试创建一个支持属性,该属性显示序列化的 int 数组(然后用于构建队列)。

我很确定这是正确的,但是 getter 总是返回一个空字符串,即使其中有值(并不是说它应该返回一个空字符串。

这是我的代码:

readonly Lazy<XmlSerializer> _queueSerializer = new Lazy<XmlSerializer>(() => new XmlSerializer(typeof(int[])));
[StringLength(1000)]
public string _MostRecentPlayers
{
    get
    {
        var stream = new MemoryStream();
        _queueSerializer.Value.Serialize(stream, _mostRecentPlayers.ToArray());
        return new StreamReader(stream).ReadToEnd();
    }
    set
    {
        if (value.IsEmpty())
        {
            _mostRecentPlayers.Clear();
            return;
        }
        MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value));
        var tempQueue = _queueSerializer.Value.Deserialize(stream) as int[];
        _mostRecentPlayers.Clear();
        tempQueue.ForEach(_mostRecentPlayers.Enqueue);
    }
}
readonly Queue<int> _mostRecentPlayers = new Queue<int>(_mostRecentAmountTracked);

Its late and fully possible I'm missing something obvious but what is it?

I'm trying to create a backing property which reveals an int array as serialized (which is then used to build up a Queue).

I'm pretty sure this is right but the getter always return a blank string, even when there are values in there (not that it should ever return a blank string.

Here is my code:

readonly Lazy<XmlSerializer> _queueSerializer = new Lazy<XmlSerializer>(() => new XmlSerializer(typeof(int[])));
[StringLength(1000)]
public string _MostRecentPlayers
{
    get
    {
        var stream = new MemoryStream();
        _queueSerializer.Value.Serialize(stream, _mostRecentPlayers.ToArray());
        return new StreamReader(stream).ReadToEnd();
    }
    set
    {
        if (value.IsEmpty())
        {
            _mostRecentPlayers.Clear();
            return;
        }
        MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value));
        var tempQueue = _queueSerializer.Value.Deserialize(stream) as int[];
        _mostRecentPlayers.Clear();
        tempQueue.ForEach(_mostRecentPlayers.Enqueue);
    }
}
readonly Queue<int> _mostRecentPlayers = new Queue<int>(_mostRecentAmountTracked);

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

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

发布评论

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

评论(1

白鸥掠海 2024-12-04 09:27:48

您还没有倒回流;它位于末尾。在读取之前设置.Position = 0。或者更简单,只需序列化为 StringWriter,或者如果您确实想使用 MemoryStream,请从 GetBuffer() 传递(超大的)后备数组> 连同.Length一起转换为Encoding并调用GetString()

using(var sw = new StringWriter()) {
    _queueSerializer.Value.Serialize(sw, _mostRecentPlayers.ToArray());
    xml = sw.ToString();
}

或者对于 ASCII(参见评论):

using(var ms = new MemoryStream()) {
    var settings = new XmlWriterSettings {
        Encoding = Encoding.ASCII
    };
    using(var xw = XmlWriter.Create(ms, settings)) {
        _queueSerializer.Value.Serialize(xw, _mostRecentPlayers.ToArray());
    }
    xml = Encoding.ASCII.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}

此外,除非您不太可能在 exe 中序列化,否则我建议简化为:

static readonly XmlSerializer _queueSerializer =new XmlSerializer(typeof(int[]));

最后,请注意,作为抛出一些内容的机制,xml 非常冗长周围的整数。 CSV 看起来更简单(假设您想要文本)。

You haven't rewound the stream; it is positioned at the end. Set .Position = 0 before reading it. Or easier, just serialize to a StringWriter, or if you really want to use a MemoryStream, pass the (oversized) backing array from GetBuffer() along with the .Length to an Encoding and call GetString().

using(var sw = new StringWriter()) {
    _queueSerializer.Value.Serialize(sw, _mostRecentPlayers.ToArray());
    xml = sw.ToString();
}

or for ASCII (see comments):

using(var ms = new MemoryStream()) {
    var settings = new XmlWriterSettings {
        Encoding = Encoding.ASCII
    };
    using(var xw = XmlWriter.Create(ms, settings)) {
        _queueSerializer.Value.Serialize(xw, _mostRecentPlayers.ToArray());
    }
    xml = Encoding.ASCII.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}

Also, unless it is unlikely that you will serialize in the exe, I would suggest simplifying to just:

static readonly XmlSerializer _queueSerializer =new XmlSerializer(typeof(int[]));

Finally, note that xml is quite verbose as a mechansim to throw some ints around. CSV would seem a lot simpler (assuming you want text).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文