【发布时间】:2011-11-19 17:31:58
【问题描述】:
假设我有以下课程:
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 经验。如果我提供任何其他有用的信息,请告诉我。
【问题讨论】:
标签: c# wpf xaml xml-serialization