0

I try to save an object to an XML Document. I always get an inner Exception Error. I think it has to do something with boxing and unboxing but i cannot help myself.

So here we go:

The Method for XML Saving

public class Saveclass
//
        public static void Save(string filename, List<Class> classlist)
        {

            StreamWriter writer = new StreamWriter(filename, false, Encoding.UTF8);
            XmlSerializer serializer = new XmlSerializer(typeof(class));
            serializer.Serialize(writer,classlist);//Here i get the error at classlist
            writer.Close();
        }

And here the SaveFile Dialog

private void SaveButton(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();

            dialog.Filter = "XML Data|*.xml";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                List<class> classlist = null;
                foreach (ListViewItem item in this.listViewWinforms.Items)
                {
                    classlist = new List<class>();
                }
                Saveclass.Save(dialog.FileName, classlist)
            }
        }

In the Basic i have a Listview with Items inside and want to save these Items in my listview to an XML Document Error is

System.InvalidOperationException: 'Trying to generate an XML Document

InvalidCastException: The object of the Type "System.Collections.Generic.List`1[Namespace.Class]" cannot cast into"Namespace.Class"

2
  • 3
    Any time you “get the error” you need to tell us what the error is. Commented Dec 1, 2019 at 19:56
  • @DourHighArch i editet it. So you can see the error. I think it has to do with boxing and unboxing Commented Dec 1, 2019 at 20:24

2 Answers 2

2

There is a type mismatch.

You are defining your serializer as:

new XmlSerializer(typeof(class));

Which is configured to serialize objects of type class.

However, you are then trying to serialize the classlist object, which is not a class but a List<Class>.

Try defining your serializer as:

new XmlSerializer(typeof(List<Class>));

By the way, naming your class class is very confusing. You should try to name your classes more descriptively.

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

Comments

0

I think you need to change just one line:

Just use typeof(List) instead of typeof(class))

XmlSerializer serializer = new XmlSerializer(typeof(List<Class>));

Here is a small example

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<Car> myList = new List<Car>();
            myList.Add(new Car(){ Name = "Beetle", Brand = "VW", Price = 5999.9M });
            myList.Add(new Car(){ Name = "Corolla", Brand = "Toyota", Price = 49999.9M });

            Saveclass.Save("carlist.xml",myList);
        }
    }

    public static class Saveclass
    {
        public static void Save(string filename, List<Car> classlist)
        {
            StreamWriter writer = new StreamWriter(filename, false, Encoding.UTF8);
            XmlSerializer serializer = new XmlSerializer(typeof(List<Car>));
            serializer.Serialize(writer,classlist);
            writer.Close();
        }
    }

    public class Car
    {
      public string Name  {get;set;}
      public string Brand  {get; set;}
      public decimal Price {get; set;}
    }
}

You can test here

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.