我正在尝试使用monotouch从SQLite数据库序列化表。我也在使用SQLite.cs包装器:
public class Invoice
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Supplier {get; set;}
public string Date {get; set;}
public string PaymentMethod {get; set;}
public string Notes {get; set;}
public Invoice(int newID)
{
Id = newID;
}
public Invoice()
{
}
}
private void GenerateXML()
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var filePath = Path.Combine(path, "wpfprototype.xml");
var invoices = GetInvoices();
XmlSerializer serializer = new XmlSerializer(typeof(Invoice));
TextWriter writer = new StreamWriter(filePath);
serializer.Serialize(writer,invoices);
}
private List<Invoice> GetInvoices()
{
TableQuery<Invoice> invoices = app.db.Table<Invoice>();
List<Invoice> final = new List<Invoice>();
int counter = 0;
while (counter < invoices.Count())
{
final.Add(invoices.ElementAt(counter));
counter++;
}
return final;
}
到达队中时:
serializer.Serialize(writer,invoices);
我得到了这个例外:
Unhandled Exception: System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type of the argument object 'System.Collections.Generic.List`1[[WpfPrototype1.Invoice, WpfPrototype1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]' is not primitive.
at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive (System.String name, System.String ns, System.Object o, Boolean xsiType) [0x00000] in <filename unknown>:0
at System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObject (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp, Boolean isNullable, Boolean needType, Boolean writeWrappingElem) [0x00000] in <filename unknown>:0
at System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteRoot (System.Object ob) [0x00000] in <filename unknown>:0
at System.Xml.Serialization.XmlSerializer.Serialize (System.Object o, System.Xml.Serialization.XmlSeError connecting stdout and stderr (127.0.0.1:10001)rializationWriter writer) [0x00000] in <filename unknown>:0
at System.Xml.Serialization.XmlSerializer.Serialize (System.Xml.XmlWriter writer, System.Object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize (System.Xml.XmlWriter writer, System.Object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces) [0x00000] in <filename unknown>:0
at System.Xml.Serialization.XmlSerializer.Serialize (System.IO.TextWriter textWriter, System.Object o) [0x00000] in <filename unknown>:0
at WpfPrototype1.MainInvoicesView.GenerateXML () [0x00000] in <filename unknown>:0
at WpfPrototype1.MainInvoicesView.<ViewDidLoad>m__6 (System.Object , System.EventArgs ) [0x00000] in <filename unknown>:0
at MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] in <filename unknown>:0
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0
at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in <filename unknown>:0
at WpfPrototype1.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0
有人知道解决方案吗?
的问候,
克劳迪奥
答案 0 :(得分:0)
我自己找到了解决方案。我在GenerateXML()方法中做了一些改动,它起作用了:
public void GenerateXML() {
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var filePath = Path.Combine(path, "wpfprototype.xml");
var invoices = app.db.Table<Invoice>().ToList();
XmlSerializer serializer = new XmlSerializer( typeof(List<Invoice>) );
TextWriter writer = new StreamWriter(filePath);
serializer.Serialize(writer,invoices);
}
感谢所有尝试解决此问题的人!