Monotouch - Сериализация XML - PullRequest
       13

Monotouch - Сериализация XML

0 голосов
/ 04 января 2011

Я пытаюсь сериализовать таблицу из базы данных SQLite, используя monotouch. Я тоже использую оболочку 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 

Кто-нибудь знает решение?

С уважением,
Claudio

1 Ответ

0 голосов
/ 04 января 2011

Я получил решение сам.Я сделал некоторые изменения в методе 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);

}

Спасибо всем, кто пытается решить эту проблему!

...