0

System.InvalidOperationException: The type of the argument object 'SI_Foodware.Model.LocalisationCollection' is not primitive. System.InvalidOperationException: There was an error generating the XML document.

LocalisationCollection.cs

using System.Xml.Serialization;

namespace SI_Foodware.Model
{
    [XmlRoot("LocalisationCollection")]
    public class LocalisationCollection
    {
        [XmlArray("LocalisationItems")]
        [XmlArrayItem("LocalisationItem", typeof(LocalisationItem))]
        public LocalisationItem[] LocalisationItem { get; set; }
    }
}

LocalisationItem.cs

using System.Xml.Serialization;
using SQLite.Net.Attributes;

namespace SI_Foodware.Model
{
    public class LocalisationItem
    {
        [PrimaryKey, AutoIncrement]
        [XmlIgnore]
        public int Id { get; set; }

        [XmlElement("Page")]
        public string Page { get; set; }

        [XmlElement("Field")]
        public string Field { get; set; }

        [XmlElement("Language")]
        public string Language { get; set; }

        [XmlElement("Value")]
        public string Value { get; set; }

        [XmlElement("Width")]
        public string Width { get; set; }

        [XmlElement("Columns")]
        public string Columns { get; set; }

        [XmlElement("Table")]
        public string Table { get; set; }

        [XmlElement("Title")]
        public string Title { get; set; }

        [XmlElement("Parent")]
        public string Parent { get; set; }

        [XmlElement("IconSource")]
        public string IconSource { get; set; }

        [XmlElement("TargetType")]
        public string TargetType { get; set; }
   }
}

Function to Serialize

    public bool Serialize(string filename)
    {
        var path = GetPath(filename);
        var serializer = new XmlSerializer(typeof(List<LocalisationCollection>));
        var writer = new FileStream(path, FileMode.Create);
        var localisationItems = db.GetAllItems<LocalisationItem>();
        var collection = new LocalisationCollection();

        collection.LocalisationItem = localisationItems.ToArray();

        try
        {
            serializer.Serialize(writer, collection);
            writer.Close();
            return true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

I want somethink like this

<?xml version="1.0" encoding="utf-8"?>
<LocalisationCollection>
    <LocalisationItems>
        <LocalisationItem>
            <Language>Nederlands</Language>
        </LocalisationItem>
        <LocalisationItem>
            <Language>Engels</Language>
        </LocalisationItem>
        <LocalisationItem>
            <Page>LoginPage</Page>
            <Field>grd_grid</Field>
            <Columns>2</Columns>
        </LocalisationItem>
        <LocalisationItem>
            <Page>LoginPage</Page>
            <Field>grd_grid</Field>
            <Width>120</Width>
        </LocalisationItem>
        <LocalisationItem>
            <Page>LoginPage</Page>
            <Field>grd_grid</Field>
            <Width>180</Width>
        </LocalisationItem>
    </LocalisationItems>
</LocalisationCollection>
Ali Eren
  • 489
  • 10
  • 23
  • Your serializer uses List but the actual object that you are passing is a LocalisationCollection. To get the desired XML output you do not need to put LocalisationColleciton into a List. – Andrew Tavera Jul 06 '16 at 20:41
  • Catching all exceptions and rethrowing a generic exception with just a message is not a good idea, as this wipes out the exception type and stack trace. See [Why catch and rethrow an exception in C#?](https://stackoverflow.com/questions/881473/why-catch-and-rethrow-an-exception-in-c) – dbc Jul 06 '16 at 20:56

1 Answers1

0

You are trying to serialize a LocalisationCollection with an XmlSerializer constructed for a List<LocalisationCollection>:

   var serializer = new XmlSerializer(typeof(List<LocalisationCollection>));
   var collection = new LocalisationCollection();
   serializer.Serialize(writer, collection);

This will not work. You must use a serializer constructed for the same type being serialized:

    var serializer = new XmlSerializer(typeof(LocalisationCollection));

To avoid this mistake, you could create the following static helper method:

public static class XmlSerializationHelper
{
    public static void SerializeToFile<T>(this T obj, string path, XmlWriterSettings settings = null, XmlSerializer serializer = null)
    {
        if (obj == null)
            throw new ArgumentNullException("obj");
        using (var stream = new FileStream(path, FileMode.Create))
        using (var writer = XmlWriter.Create(stream, settings))
        {
            serializer = serializer ?? new XmlSerializer(obj.GetType());
            serializer.Serialize(writer, obj);
        }
    }
}

Then your Serialize method becomes:

    public bool Serialize(string filename)
    {
        var path = GetPath(filename);
        var localisationItems = db.GetAllItems<LocalisationItem>();
        var collection = new LocalisationCollection { LocalisationItem = localisationItems.ToArray() };

        collection.SerializeToFile(path);

        return true;
    }
dbc
  • 104,963
  • 20
  • 228
  • 340