System.InvalidOperationException:参数对象'SI_Foodware.Model.LocalisationCollection'的类型不是原始的。 System.InvalidOperationException:生成XML文档时出错。
using System.Xml.Serialization;
namespace SI_Foodware.Model
{
[XmlRoot("LocalisationCollection")]
public class LocalisationCollection
{
[XmlArray("LocalisationItems")]
[XmlArrayItem("LocalisationItem", typeof(LocalisationItem))]
public LocalisationItem[] LocalisationItem { get; set; }
}
}
using System.Xml.Serialization;
using SQLite.Net.Attributes;
namespace SI_Foodware.Model
{
public class LocalisationItem
{
[PrimaryKey, AutoIncrement]
[XmlIgnore]
public int Id { get; set; }
[XmlElement("Page")]
public string Page { get; set; }
[XmlElement("Field")]
public string Field { get; set; }
[XmlElement("Language")]
public string Language { get; set; }
[XmlElement("Value")]
public string Value { get; set; }
[XmlElement("Width")]
public string Width { get; set; }
[XmlElement("Columns")]
public string Columns { get; set; }
[XmlElement("Table")]
public string Table { get; set; }
[XmlElement("Title")]
public string Title { get; set; }
[XmlElement("Parent")]
public string Parent { get; set; }
[XmlElement("IconSource")]
public string IconSource { get; set; }
[XmlElement("TargetType")]
public string TargetType { get; set; }
}
}
public bool Serialize(string filename)
{
var path = GetPath(filename);
var serializer = new XmlSerializer(typeof(List<LocalisationCollection>));
var writer = new FileStream(path, FileMode.Create);
var localisationItems = db.GetAllItems<LocalisationItem>();
var collection = new LocalisationCollection();
collection.LocalisationItem = localisationItems.ToArray();
try
{
serializer.Serialize(writer, collection);
writer.Close();
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
我想要这样的想法
<?xml version="1.0" encoding="utf-8"?>
<LocalisationCollection>
<LocalisationItems>
<LocalisationItem>
<Language>Nederlands</Language>
</LocalisationItem>
<LocalisationItem>
<Language>Engels</Language>
</LocalisationItem>
<LocalisationItem>
<Page>LoginPage</Page>
<Field>grd_grid</Field>
<Columns>2</Columns>
</LocalisationItem>
<LocalisationItem>
<Page>LoginPage</Page>
<Field>grd_grid</Field>
<Width>120</Width>
</LocalisationItem>
<LocalisationItem>
<Page>LoginPage</Page>
<Field>grd_grid</Field>
<Width>180</Width>
</LocalisationItem>
</LocalisationItems>
</LocalisationCollection>
答案 0 :(得分:0)
您正尝试序列化LocalisationCollection
XmlSerializer
为List<LocalisationCollection>
构建:
var serializer = new XmlSerializer(typeof(List<LocalisationCollection>));
var collection = new LocalisationCollection();
serializer.Serialize(writer, collection);
这不起作用。您必须使用为序列化的相同类型构造的序列化程序:
var serializer = new XmlSerializer(typeof(LocalisationCollection));
为避免此错误,您可以创建以下静态辅助方法:
public static class XmlSerializationHelper
{
public static void SerializeToFile<T>(this T obj, string path, XmlWriterSettings settings = null, XmlSerializer serializer = null)
{
if (obj == null)
throw new ArgumentNullException("obj");
using (var stream = new FileStream(path, FileMode.Create))
using (var writer = XmlWriter.Create(stream, settings))
{
serializer = serializer ?? new XmlSerializer(obj.GetType());
serializer.Serialize(writer, obj);
}
}
}
然后您的Serialize
方法变为:
public bool Serialize(string filename)
{
var path = GetPath(filename);
var localisationItems = db.GetAllItems<LocalisationItem>();
var collection = new LocalisationCollection { LocalisationItem = localisationItems.ToArray() };
collection.SerializeToFile(path);
return true;
}