WPF中的XML序列化问题
假设我有以下类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NetworkSwitcher
{
[Serializable]
class testClass
{
public string str;
public testClass(string _str)
{
this.str = _str;
}
}
}
现在,当我尝试执行以下类时,它会抛出 System.Windows.Markup.XamlParseException。
testClass tc = new testClass("Hello World");
XmlSerializer xsl = new XmlSerializer(typeof(testClass));
TextWriter WriteFileStream = new StreamWriter(@"C:\NSProfiles.xml");
xsl.Serialize(WriteFileStream, tc);
WriteFileStream.Close();
如果我使用简单的 String 类型对象而不是 tectClass,代码可以正常工作:
string data = "hello world";
XmlSerializer xsl = new XmlSerializer(typeof(String));
TextWriter WriteFileStream = new StreamWriter(@"C:\NSProfiles.xml");
xsl.Serialize(WriteFileStream, data);
WriteFileStream.Close();
所以我猜问题出在类定义中,我该如何修复它?我使用的是 WPF,而不是 WinForms,而且我之前没有任何 WPF 或 XMLSerialization 经验。如果我需要提供任何其他有用的信息,请告诉我。
Suppose I have the following class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NetworkSwitcher
{
[Serializable]
class testClass
{
public string str;
public testClass(string _str)
{
this.str = _str;
}
}
}
Now when I try to execute the following, it throws a System.Windows.Markup.XamlParseException.
testClass tc = new testClass("Hello World");
XmlSerializer xsl = new XmlSerializer(typeof(testClass));
TextWriter WriteFileStream = new StreamWriter(@"C:\NSProfiles.xml");
xsl.Serialize(WriteFileStream, tc);
WriteFileStream.Close();
If I use a simple String type object instead of tectClass, the code works fine:
string data = "hello world";
XmlSerializer xsl = new XmlSerializer(typeof(String));
TextWriter WriteFileStream = new StreamWriter(@"C:\NSProfiles.xml");
xsl.Serialize(WriteFileStream, data);
WriteFileStream.Close();
So I guess the problem is in the class definition, how can I fix it? I am using WPF, not WinForms and I don't have any prior experience in WPF or XMLSerialization. Let me know if I shall provide any other useful information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要修复该错误,请向类添加默认构造函数(不带参数的构造函数)。
单独执行此操作将阻止错误发生,但可能无法正确序列化字符串值。我对此不是 100% 确定,因为它可能能够序列化公共成员变量。
如果没有,您需要为字符串值添加一个公共属性。
此类的更“标准”实现可能如下所示。您可以拥有第二个构造函数,也可以仅使用 setter 来设置值:
编辑:向类添加 public 修饰符。现在应该可以工作了。
另外,我猜您正在调用在 Window 的构造函数中引发异常的代码?这是我看到这会引发 XAML 异常的唯一原因。构造函数中的错误会包含在该 XAML 异常中,因此在这些情况下,您需要查看 InnerException 来查找问题。
To fix the error, add a default constructor to the class (a constructor that takes no arguments).
Doing this alone will stop the error from happening, but may not serialize the string value correctly. I'm not 100% sure on that, as it may be able to serialize a public member variable.
If not, you'll want to add a public Property for the string value.
A more "standard" implementation of this class would likely look like the following. You would either have a second constructor or just use the setter to set the value:
Edit: Added the public modifier to the class. Should work now.
Also, I am guessing you are calling the code that throws the exception in the constructor of the Window? That is the only reason I can see that this would throw a XAML exception. Errors in the constructor get wrapped in that XAML exception, so in these cases you want to look at the InnerException to find the problem.
这些解决了问题。
向类添加公共类修饰符。
These solved the problem.
Adding public class modifier to the class.
开始阅读有关 Xml 序列化的文档。
您错过了:
虽然无法确定 XAML 异常从何而来,但这个异常毫无意义。
Start reading the documentation on Xml Serialization.
You miss:
Nlot sure where your XAML exception comes from, though, this one makes NO sense.