gpt4 book ai didi

c# - 为什么反序列化不起作用?

转载 作者:行者123 更新时间:2023-11-30 22:15:17 25 4
gpt4 key购买 nike

我通常使用(反)序列化。我以前从未遇到过问题,但我认为这只是我看不到的人为错误......序列化工作完美,但反序列化不完美。

这是我的代码:

using System;
using System.IO;
using System.Windows.Forms;
using Utils;

using System.Xml;
using System.Xml.Serialization;

namespace Tests
{
public partial class MainForm : Form
{
private Test test;

public MainForm()
{
InitializeComponent();
}

private void SaveButton_Click(object sender, EventArgs e)
{
try
{
test = new Test();
test.MyInt = int.Parse(MyIntTextBox.Text);
test.MyStr = MyStrTextBox.Text;
test.Save();
MessageBox.Show("Serialized!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception caught", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void LoadButton_Click(object sender, EventArgs e)
{
try
{
test = Test.Load();
MyIntTextBox.Text = test.MyInt.ToString();
MyStrTextBox.Text = test.MyStr;
MessageBox.Show("Deserialized!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception caught", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

}

[Serializable]
public class Test
{
public string MyStr { set; get; }
public int MyInt { set; get; }

public void Save()
{
using (StreamWriter sw = new StreamWriter("TestSerialized.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(Test));
xs.Serialize(sw, this);
sw.Flush();
}
}

static public Test Load()
{
Test obj = null;
using (StreamReader sr = new StreamReader("TestSerialized.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(Test));
if (!xs.CanDeserialize(XmlReader.Create(sr)))
throw new NotSupportedException(string.Format("The file <{0}> cannot be loaded.", "TestSerialized.xml"));
obj = (Test)xs.Deserialize(sr);
}
return (obj);
}
}
}

这是一个基本表单,有 2 个文本框,一个用于 Test 类的每个属性和 2 个按钮,一个用于保存,另一个用于加载。

别担心,我确保有一个文件要加载;)

我想制作一个通用的(反)serliazer 类,例如:

public class Serializer<T>
{
static public void Serialize(object obj, string path)
{
using (StreamWriter sw = new StreamWriter(path))
{
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(sw, obj);
sw.Flush();
}
}

static public T Dezerialize(string path)
{
T obj;
using (StreamReader sr = new StreamReader(path))
{
XmlSerializer xs = new XmlSerializer(typeof(T));
if (!xs.CanDeserialize(XmlReader.Create(sr)))
throw new NotSupportedException(string.Format("The file <{0}> cannot be loaded.", path));
obj = (T)xs.Deserialize(sr);
}
return (obj);
}
}

但我有同样的问题:反序列化不起作用...

编辑:我捕获了一个异常:“它在 XML 文档 (0, 0) 中存在错误”以及我要反序列化的 XML 文档(它由 XmlSerializer 生成)

<?xml version="1.0" encoding="utf-8"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyStr>toto</MyStr>
<MyInt>45</MyInt>
</Test>

感谢您的帮助!

最佳答案

去掉那两条线

if (!xs.CanDeserialize(XmlReader.Create(sr))
throw new NotSupportedException(string.Format("The file <{0}> cannot be loaded.", path));

或在检查是否可以反序列化后重置流

关于c# - 为什么反序列化不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18067692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com