【发布时间】:2011-04-17 02:50:36
【问题描述】:
我正在尝试使用链接序列化哈希表
XML serialization of hash table(C#3.0)
但我收到的错误是
不应使用数据合同名称“AllMyHashtable:http://schemas.datacontract.org/2004/07/WpfApplication3”键入“WpfApplication3.MyHashtable”。将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。
我的程序如下
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MyHashtable ht = new MyHashtable();
DateTime dt = DateTime.Now;
for (int i = 0; i < 10; i++)
ht.Add(dt.AddDays(i), i);
SerializeToXmlAsFile(typeof(Hashtable), ht);
}
private void SerializeToXmlAsFile(Type targetType, Object targetObject)
{
try
{
string fileName = @"C:\output.xml";
DataContractSerializer s = new DataContractSerializer(targetType);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
using (XmlWriter w = XmlWriter.Create(fileName, settings))
{
s.WriteObject(w, targetObject);
w.Flush();
}
}
catch (Exception ex) { throw ex; }
}
}
[CollectionDataContract(Name = "AllMyHashtable", ItemName = "MyEntry",
KeyName = "MyDate", ValueName = "MyValue")]
public class MyHashtable : Dictionary<DateTime, int> { }
}
What wrong is hapenning ..please help.
谢谢
【问题讨论】:
标签: wpf c#-3.0 xml-serialization