C# 这里有一个神秘的例外:XamlParseException

C# 这里有一个神秘的例外:XamlParseException,c#,wpf,xaml,xamlparseexception,C#,Wpf,Xaml,Xamlparseexception,这是我的密码: public partial class MainWindow : Window { List<Cliente> CContent; string mainPath = @"D:\70-536\Clientes.dat"; public MainWindow() { InitializeComponent(); Do(); } private void Do() {

这是我的密码:

    public partial class MainWindow : Window
{
    List<Cliente> CContent;
    string mainPath = @"D:\70-536\Clientes.dat";

    public MainWindow()
    {
        InitializeComponent();
        Do();
    }

    private void Do()
    {

        FileInfo fi = new FileInfo(mainPath);
        if (fi.Exists)
        {
            CContent = ReturnListOfPersistentFile<Cliente>(mainPath);
        }
        else
        {
            CContent = new List<Cliente>();
        }
    }

    public List<T> ReturnListOfPersistentFile<T> (string Filename)
    {
        SoapFormatter sf = new SoapFormatter();

        using (Stream fStream = new FileStream(Filename,FileMode.Open, FileAccess.Read,FileShare.None))
        {
            List<T> list = new List<T>();
            list = (List<T>)sf.Deserialize(fStream);

            return list;
        }

    }
公共部分类主窗口:窗口
{
清单内容;
字符串mainPath=@“D:\70-536\Clientes.dat”;
公共主窗口()
{
初始化组件();
Do();
}
私人无效Do()
{
FileInfo fi=新的FileInfo(mainPath);
如果(fi.存在)
{
CContent=持久文件的返回列表(mainPath);
}
其他的
{
CContent=新列表();
}
}
公共列表ReturnListOfPersistentFile(字符串文件名)
{
SoapFormatter sf=新的SoapFormatter();
使用(Stream fStream=newfilestream(文件名,FileMode.Open,FileAccess.Read,FileShare.None))
{
列表=新列表();
list=(list)sf.反序列化(fStream);
退货清单;
}
}
这是我的innerexception的堆栈跟踪:

en System.Xml.XmlTextReaderImpl.Throw(异常e)en System.Xml.XmlTextReaderImpl.Throw(String res,String arg)en System.Xml.XmlTextReaderImpl.ThrowUnclosedElements()en System.Xml.XmlTextReaderImpl.ParseAttributes()en System.Xml.XmlTextReaderImpl.ParseElement()en System.Xml.XmlTextReaderImpl.ParseDocumentContent()en System.Xml.XmlTextReaderImpl.Read()en System.Xml.XmlTextReader.Read()en System.Runtime.Serialization.Formatters.Soap.SoapParser.ParseXml()
en System.Runtime.Serialization.Formatters.Soap.SoapParser.Run()en System.Runtime.Serialization.Formatters.Soap.ObjectReader.Deserialize(HeaderHandler 处理程序,ISerParser(语法分析器)en System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(流) 序列化流,HeaderHandler处理程序)en System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(流) 序列化流)en Solution1.MainWindow.ReturnListOfPersistentFile[T](字符串文件名)en c:\users\u201114160\documents\visualstudio 2010\Projects\Solution1\Solution1\MainWindow.xaml.cs:línea 99 en 解决方案1.MainWindow.Do()en c:\users\u201114160\documents\visual studio 2010\Projects\Solution1\Solution1\MainWindow.xaml.cs:línea 45
en Solution1.MainWindow..ctor()en c:\users\u201114160\documents\visualstudio 2010\Projects\Solution1\Solution1\MainWindow.xaml.cs:línea 36

它让我发疯,一些想法?这个例外被抛出是因为“ReturnListOfPersistentFile”通用方法,但为什么呢

提前谢谢

回答: 我试图用SOAP格式化程序序列化泛型列表,但SOAP格式化程序不支持泛型列表

我在上一篇文章中遇到了这个问题,让我引用一下微软员工的话:

我们决定不为Whidbey中的SoapFormatter投资任何重要的新功能


谢谢大家。

查看stacktrace,我注意到ThrowUnclosedElements方法。这让我觉得您的文件已损坏或格式错误

正如你自己所指出的,SOAP序列化不支持泛型列表。据我所知,这意味着你试图序列化一个列表,结果得到了一个无法反序列化的文件。这在前面已经讨论过,还有(你自己的引用)其他地方

一个解决方案可能是使用一种非泛型的、过时的、被遗忘的方式。当然,您需要访问这些项并将它们转换为适当的类型,这可以通过包装器类来完成


另一种方法是对每个对象序列化对象,而不是序列化列表。

…因为您的文件已损坏?不太可能,因为SOAP序列化不支持通用列表:(对不起,伙计们,我环顾四周,并没有发现任何与我类似的问题。无论如何,我必须解释我是如何解决它的。感谢我在谷歌上搜索了通用列表和SOAP序列化,我找到了那个网站。我很感谢您的详细回复。