I'm trying to serialize a table from a SQLite database using monotouch. I'm using the SQLite.cs wrapper too:
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;
}
When arrives in the line:
serializer.Serialize(writer,invoices);
I get this exception:
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 () [0x开发者_Python百科00000] 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
Does anybody know a solution?
Regards,
ClaudioI got a solution by myself. I made some alterations in GenerateXML() method and it worked:
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);
}
Thanks for all that try to solve this problem!
精彩评论