XmlSerializer导致XamlParseException

时间:2015-03-18 13:18:57

标签: c# xml wpf exception serialization

我有一个简单的WPF应用程序,当我尝试使用XmlSerializer序列化一个简单的Exception时,我得到一个XamlParseException。

MainWindow.xaml中的代码是:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>

</Grid>
</Window>

App.xaml中的代码是:

<Application x:Class="WpfApplication2.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>

用于序列化的Serializer静态类的代码是:

public static class Serializer<T>
{
    /// <summary>
    /// serialize an instance to xml
    /// </summary>
    /// <param name="instance"> instance to serialize </param>
    /// <returns> instance as xml string </returns>
    public static string Serialize(T instance)
    {
        StringBuilder sb = new StringBuilder();
        XmlWriterSettings settings = new XmlWriterSettings();

        using (XmlWriter writer = XmlWriter.Create(sb, settings))
        {
            XmlSerializer serializer = new XmlSerializer(instance.GetType());
            serializer.Serialize(writer, instance);
        }

        return sb.ToString();
    }

    /// <summary>
    /// deserialize an xml into an instance
    /// </summary>
    /// <param name="xml"> xml string </param>
    /// <returns> instance </returns>
    public static T Deserialize(string xml)
    {
        using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
        {
            foreach (Type t in types)
            {
                XmlSerializer serializer = new XmlSerializer(t);
                if (serializer.CanDeserialize(reader))
                    return (T)serializer.Deserialize(reader);
            }
        }

        return default(T);
    }

    /// <summary>
    /// store all derived types of T:
    /// is used in deserialization
    /// </summary>
    private static Type[] types = AppDomain.CurrentDomain.GetAssemblies()
                                        .SelectMany(s => s.GetTypes())
                                        .Where(t => typeof(T).IsAssignableFrom(t)
                                            && t.IsClass
                                            && !t.IsGenericType)
                                            .ToArray();
}

MainWindow.xaml.cs中的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
  /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Exception e = new System.IO.IOException("blablabla", new System.IO.DirectoryNotFoundException());
        MessageBox.Show(Serializer<Exception>.Serialize(e));
    }
   }
  }

当我运行这个时,我得到了这个例外:

  

'在类型上调用构造函数   与指定绑定匹配的'WpfApplication2.MainWindow'   约束引发了例外。行号“3”和行位置   '9'。

可能是什么问题?

1 个答案:

答案 0 :(得分:2)

如果您将其作为孤立的问题进行了测试,那么您会看到XmlSerializer会抛出InvalidOperationException消息

  

有一个错误反射类型'System.IO.IOException'。

内部异常详细描述了原因:

  

无法序列化System.Collections.IDictionary类型的成员System.Exception.Data,...

无法使用.NET XML Serializer序列化字典。就是这样。