2011-01-04 10 views
0

私はモノトゥッチを使用してSQLiteデータベースからテーブルをシリアル化しようとしています。私もSQLite.csラッパーを使用しています:Monotouch - XMLをシリアライズ

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

私はちょうどそれを入れました:) – Claudio

答えて

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); 

} 

感謝を!

関連する問題