为什么反序列化没有';不起作用
本文关键字:不起作用 反序列化 为什么 | 更新日期: 2023-09-27 18:26:35
我通常使用(反)序列化。我以前从未遇到过问题,但我认为这只是人为的错误,我不认为。。。序列化非常有效,但反序列化却不行。
这是我的代码:
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);
}
}
}
这是一个基本表单,有两个文本框,一个用于Test类的每个属性,还有两个按钮,一个用来保存,另一个用来加载。
别担心,我一定要加载一个文件;)
我想创建一个通用(de)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>
谢谢你的帮助!
将这2行从中删除
if (!xs.CanDeserialize(XmlReader.Create(sr))
throw new NotSupportedException(string.Format("The file <{0}> cannot be loaded.", path));
或者在检查是否可以取消分类后重置流
您的代码因此无法工作:
if (!xs.CanDeserialize(XmlReader.Create(sr)))
throw new NotSupportedException(string.Format("The file <{0}> cannot be loaded.", "TestSerialized.xml"));
在这之后,就没有什么可以从sr
中读取的了,只有在那时你才试图反序列化——这就是它失败的原因。删除此代码。
http://social.msdn.microsoft.com/Forums/en-US/f5a5ff54-3743-400c-b14d-92dae95f3605/there-is-an-error-in-xml-document-0-0-error-while-xml-deserialization遵循这个链接,它有确切的原因和解决方案。
"在您写入流后,它位于您写入的XML之后的末尾。如果您尝试从同一个流中读取,则找不到根元素。因此,如果流支持,则需要重新定位流(stream.Position=0),或者您需要在写入流后关闭流,并打开一个新的流进行读取。"