4

I get these errors when I try to XML serialize an array of lists.

Unable to generate a temporary class (result=1). error CS1026: ) expected error CS1002: ; expected ... error CS1525: Invalid expression term ')' error CS1002: ; expected

Here's my code:

This is the method that triggers the exception.

public static string SerializeToString<T>(T obj)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    using (StringWriter writer = new StringWriter())
    {
        serializer.Serialize(writer, obj);
        return writer.ToString();
    }
}

And this is the var passed to it:

List<Transaction>[] allTransactions = new List<Transaction>[20];

Thanks for any help.

[UPDATE]

public class TransactionCollection
{
    public List<Transaction>[] transactions;

    public TransactionCollection()
    {
    }

    public void Set(List<Transaction>[] t)
    {
        transactions = t;
    }
}
2
  • First of all, the xml serializer will be looking for a root element, instead of passing your array, pass a single object encapsulating your collection. Commented Apr 30, 2012 at 23:54
  • OK I created a class that encapsulated the array of transaction lists as shown above but I get the same error. Commented May 1, 2012 at 0:07

2 Answers 2

1

I don't know exactly; the error is sort of dismaying.

But if you'd rather get something done instead of diagnosing the problem, then just convert the list to an array:

    public void Run()
    {
        var allTransactions = new List<Transaction>[20];
        for (int i=0; i < allTransactions.Length; i++)
        {
            allTransactions[i] = new List<Transaction>();
        }
        var a = Array.ConvertAll(allTransactions, x => x.ToArray());
        var s = SerializeToString(a);
        System.Console.WriteLine("{0}", s);
    }

result:

<ArrayOfArrayOfTransaction>
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
  <ArrayOfTransaction />
</ArrayOfArrayOfTransaction>

ps: You do not need to "wrap" arrays into containing types in order to serialize them.

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

1 Comment

This works for me as well. If OP is actually passing empty data I could see it causing errors.
0

This works for me:

[XmlRoot("TransactionCollection")]
public class TransactionCollection
{
    private List<List<Transaction>> _lst = new List<List<Transaction>>();   

    [XmlArrayItem()]
    public List<List<Transaction>> Transactions { get { return _lst; } }

    public TransactionCollection()
    {
    }
}

[XmlRoot("Transaction")]
public class Transaction
{
    [XmlElement("Id")]
    public string Id = String.Empty;
}

Testing:

void Main()
{
    var transactions1 = new List<Transaction> { new Transaction(), new Transaction() };
    var transactions2 = new List<Transaction> { new Transaction(), new Transaction() };
    var trans = new TransactionCollection();
    trans.Transactions.Add(transactions1);
    trans.Transactions.Add(transactions2);
    SerializeToString(trans);
}

Note that this is because the XmlSerializer was able to work out that Lists should be serialised as array items. You could also use a double array of transactions rather than a double list:

[XmlArrayItem()]
public Transaction[][] Transactions { get; set; }

If you really need to use an Array and a List as in the original example, you need to make your List XML serialisable:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.