0

This is my code so far:

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Xml;

namespace Opgaver
{
     class OpgaveController
    {
        static ArrayList _arrAktiveOpgaver = new ArrayList();
        public ArrayList arrAktiveOpgaver
        {
            get { return _arrAktiveOpgaver; }
            set { _arrAktiveOpgaver = value; }
        }

        static ArrayList _arrAfsluttedeOpgaver = new ArrayList();
        public ArrayList arrAfsluttedeOpgaver
        {
            get { return _arrAfsluttedeOpgaver; }
            set { _arrAfsluttedeOpgaver = value; }
        }

        static int _opgavenr = 100;
        public int opgavenr
        {
            get { return _opgavenr; }
            set { _opgavenr = value; }
        }

        public void opretOpgave(string opgavenavn, string opgavebeskrivelse, int opgaveprioritet, DateTime opgaveafsluttetdato)
        {
            DateTime oprettetdato = new DateTime();
            oprettetdato = DateTime.Now;
            bool opgaveafsluttet = false;
            Opgave opgave = new Opgave(oprettetdato, _opgavenr, opgavenavn, opgavebeskrivelse, opgaveprioritet, opgaveafsluttet, opgaveafsluttetdato);

            if (opgave.opgaveAfsluttet == false)
            {
                _arrAktiveOpgaver.Add(opgave);
            }
            else
            {
                _arrAfsluttedeOpgaver.Add(opgave);
            }

            _opgavenr++;
        }

        public OpgaveController()
        {

        }



        }

        public void test()
        { 
            string outfile = @"C:\Folder\Tester.xml";

            XmlSerializer xs;

            Serialize<List<Opgave>>(arrAktiveOpgaver, outfile);
            arrAktiveOpgaver = <List<Opgave>>(outfile);//deserialize data - Generates this error: Error 2   Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments   P:\Programmerings opgaver\Opgaver\Opgaver\OpgaveController.cs   107 33  Opgaver

        }

        private void Serialize<T1>(ArrayList arrAktiveOpgaver, string outfile)
        {
            throw new NotImplementedException();
        }

        public static T DeserializeFromXml<T>(string inputFile)
        {
            XmlSerializer s = new XmlSerializer(typeof(T));
            T deserializedObject = default(T);

            using (TextReader textReader = new StreamReader(inputFile))
            {
                deserializedObject = (T)s.Deserialize(textReader);
                textReader.Close();
            }

            return deserializedObject;
        }

        public static void SerializeToXml<T>(T objToSerialize, string outputFile)
        {

            XmlSerializer s = new XmlSerializer(objToSerialize.GetType());

            using (TextWriter textWriter = new StreamWriter(outputFile))
            {
                s.Serialize(textWriter, objToSerialize);
                textWriter.Close();
            }
        }

    }
}

i need to serialize the arraylist, and deserialize it..

and here is my opgave class:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Opgaver
{
    [Serializable()] public class Opgave : IComparable
    {
        DateTime _oprettetDato = new DateTime();
        public DateTime oprettetDato
        {
            get { return _oprettetDato; }
            set { _oprettetDato = value; }
        }

        int _opgavenr;
        public int opgavenr
        {
            get { return _opgavenr; }
            set { _opgavenr = value; }
        }

        string _opgaveNavn;
        public string opgaveNavn
        {
            get { return _opgaveNavn; }
            set { _opgaveNavn = value; }
        }

        string _opgaveBeskrivelse;
        public string opgaveBeskrivelse
        {
            get { return _opgaveBeskrivelse; }
            set { _opgaveBeskrivelse = value; }
        }

        int _opgavePrioritet;
        public int opgavePrioritet
        {
            get { return _opgavePrioritet; }
            set { _opgavePrioritet = value; }
        }

        bool _opgaveAfsluttet = false;
        public bool opgaveAfsluttet
        {
            get { return _opgaveAfsluttet; }
            set { _opgaveAfsluttet = value; }
        }

        DateTime _opgaveAfsluttetDato = new DateTime();
        public DateTime opgaveAfsluttetDato
        {
            get { return _opgaveAfsluttetDato; }
            set { _opgaveAfsluttetDato = value; }
        }

        public Opgave()
        {
        }

        public Opgave(DateTime oprettetdato, int opgavenr, string opgavenavn, string opgavebeskrivelse, int opgaveprioritet, bool opgaveafsluttet, DateTime opgaveafsluttetdato)
        {
            _oprettetDato = oprettetdato;
            _opgavenr = opgavenr;
            _opgaveNavn = opgavenavn;
            _opgaveBeskrivelse = opgavebeskrivelse;
            _opgavePrioritet = opgaveprioritet;
            _opgaveAfsluttet = opgaveafsluttet;
            _opgaveAfsluttetDato = opgaveafsluttetdato;
        }

        //Sorterings metode
        public int CompareTo(object obj)
        {
            Opgave Compare = (Opgave)obj;
            int result = this.opgavenr.CompareTo(Compare.opgavenr);
            if (result == 0)
                result = this.opgavenr.CompareTo(Compare.opgavenr);
            return result;
        }
    }
}
1
  • 1
    That isn't a valid xml document. An xml file can have only one root element. It could be an xml fragment, but few things work with fragments Commented Jul 28, 2011 at 7:44

2 Answers 2

1

Use this static methods whenver you want to serialize or deserialize data: example:

public void test()
{ 
    string outfile = @"C:\Folder\Tester.xml";

    SerializeToXml<List<Opgaver>>(arrAktiveOpgaver, outfile);//serialize data

    arrAktiveOpgaver = DeserializeFromXml<List<Opgaver>>(outfile);//deserialize data
}

public static T DeserializeFromXml<T>(string inputFile)
{
    XmlSerializer s = new XmlSerializer(typeof(T));
    T deserializedObject = default(T);

    using (TextReader textReader = new StreamReader(inputFile))
    {
        deserializedObject = (T)s.Deserialize(textReader);
        textReader.Close();
    }

    return deserializedObject;
}

public static void SerializeToXml<T>(T objToSerialize, string outputFile)
{

    XmlSerializer s = new XmlSerializer(objToSerialize.GetType());

    using (TextWriter textWriter = new StreamWriter(outputFile))
    {
        s.Serialize(textWriter, objToSerialize);
        textWriter.Close();
    }
}
Sign up to request clarification or add additional context in comments.

19 Comments

@Marc: Sorry but I didn't get your point. I made them generic and static so he can use them for any ordinary xml serializaion and deserialization. for instance if her wrap the List<Opgave> In a serializable class, he can still use this static methods...
hmm Thx for the code Jalal.. but im getting this error: Using the generic type 'system.collections.gerneric.list<t> requires 1 type arguments
@Mathias: Did you pass a Opgave to the List<> so it should be List<Opgave>, Just copy the code I putted in the test() method and try it.
yeah ive copied the code.. the only thing that missed it a ";" after the serialize line.. but im still getting the error..
is it becuase im using an arraylist?
|
1

Serialize array of Opgave instead of serializing them in loop.

XmlSerializer xs = new XmlSerializer(arrAktiveOpgaver.GetType());
xs.Serialize(sw, arrAktiveOpgaver);

And then deserialize as array of Opgave.

5 Comments

i chaged the serializer to this: public void test() { string outfile = @"C:\Folder\Tester.xml"; StreamWriter sw = new StreamWriter(outfile); XmlSerializer xs = new XmlSerializer(arrAktiveOpgaver.GetType()); xs.Serialize(sw, arrAktiveOpgaver); sw.Close(); } but then i get this error : System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type Opgaver.Opgave was not expected. Use the XmlInclude or SoapInclude attribute to specify types
@Mathias what exactly is the type of arrAktiveOpgaver ?
@Mathias, Maybe this question would be helpful; stackoverflow.com/questions/988737/…
arrAktiveOpgaver is an ArrayList
@Mathias, Update your question with source code, sow we can reproduce you issue.

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.