0

I'm using SQLite.cs wrapper for helping me with the database and I have this method for create XML from a table, that is working fine.

public void GenerateInvoiceXML(string filePath) {
        var invoices = app.db.Table<Invoice>().ToList();

        XmlSerializer serializer = new XmlSerializer( typeof(List<Invoice>) );
        TextWriter writer = new StreamWriter(filePath);

        serializer.Serialize(writer,invoices);
        writer.Close();

}

All tables that I have are defined like this:

[Serializable]  
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()
    {

    }
}

But I want to change this method for something like this:

public void GenerateInvoiceXML(string filePath, Type table) {
        var dataForXML = app.db.Table<table>().ToList();

        XmlSerializer serializer = new XmlSerializer( typeof(List<table>) );
        TextWriter writer = new StreamWriter(filePath);

        serializer.Serialize(writer,dataForXML);
        writer.Close();     
}

Does anybody have an idea how to do it?

Kind Regards,
Claudio

2 Answers 2

6

Try this:

public void GenerateInvoiceXML<TTable>(string filePath) {
    var dataForXML = app.db.Table<TTable>().ToList();

    XmlSerializer serializer = new XmlSerializer( typeof(List<TTable>) );
    TextWriter writer = new StreamWriter(filePath);

    serializer.Serialize(writer,dataForXML);
    writer.Close();     
}

In order to pass a generic type argument you must be able to specify it a compile time - in order to do this you must make your GenerateInvoiceXML method generic as I have shown above.

Sign up to request clarification or add additional context in comments.

3 Comments

+1: If app.db.Table<T> has any constraints (which is likely), you'll also need to add those constraints to this method as well.
thanks a lot. Do you know any site/reference about this that I could read something more? I'm having a lot of problems with this and I don't even know how to search for it.
The best description/discussion I have ever read on C# generics in Jon Skeet's C# In Depth: amazon.com/C-Depth-Second-Jon-Skeet/dp/1935182471. Unfortunately I am not aware of any free/online equivalents - buy this book, you won't regret it.
2

You can try this:

public void GenerateXML<T>(string filePath)
{
    var dataForXML = app.db.Table<T>().ToList();
    XmlSerializer serializer = new XmlSerializer( typeof(List<T>) );
    TextWriter writer = new StreamWriter(filePath);
    serializer.Serialize(writer,dataForXML);
    writer.Close();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.