1
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace AppPrueba
{
    public partial class Form1 : Form
    {
        ArrayList listaFilas = new ArrayList();
        public Form1()
        {
            InitializeComponent();
        }

        List<Empleados> emp = new List<Empleados>();
        List<Agenda> agen = new List<Agenda>();

        static public void SerializeToXML(List<Agenda> agen)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Agenda>));
            TextWriter textWriter = new StreamWriter(@"C:\agenda.xml");
            serializer.Serialize(textWriter, agen);
            textWriter.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog(this);
            string strfilename = openFileDialog1.FileName;

            txtPath.Text = strfilename;

            if ((strfilename.Trim().Length > 0) && (File.Exists(strfilename)))
            {
                string[] readText = File.ReadAllLines(strfilename);

                if (strfilename.EndsWith("Agenda.txt"))
                {
                    lstLinesBeforeChange.Items.Clear();
                    foreach (string s in readText)
                    {
                        lstLinesBeforeChange.Items.Add(s);
                    }
                }
                else
                {
                    lstLinesBeforeChange.Items.Clear();
                    foreach (string s in readText)
                    {
                        lstLinesBeforeChange.Items.Add(s);
                    }
                }
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            string strfilename = txtPath.Text;

            string[] readText = File.ReadAllLines(strfilename);

            if (strfilename.EndsWith("Agenda.txt"))
            {
                lstLinesAfterChange.Items.Clear();

                foreach (string s in readText)
                {
                    int nroEmp = Convert.ToInt32(s.Substring(0, 4));
                    string nombre = s.Substring(4, 15);
                    string telef = s.Substring(19, 10);
                    string ciudad = s.Substring(29);

                    Agenda unAgenda = new Agenda();

                    unAgenda.NroEmp = nroEmp;
                    unAgenda.Nombre = nombre.TrimStart().TrimEnd();
                    unAgenda.Telefono = telef;
                    unAgenda.Localidad = ciudad;

                    agen.Add(unAgenda);

                    agen.Sort(delegate(Agenda a1, Agenda a2)
                    {
                        return a1.NroEmp.CompareTo(a2.NroEmp);
                    });
                }

                foreach (Agenda a in agen)
                {
                    string agenOrd = a.NroEmp.ToString() + "\t" + a.Nombre + "\t" + a.Telefono + "\t" + a.Localidad;

                    lstLinesAfterChange.Items.Add(agenOrd);
                }
            }
            else
            {
                lstLinesAfterChange.Items.Clear();
                foreach (string s in readText)
                {
                    string[] sSinBarra = s.Split('|');

                    int nroEmp = Convert.ToInt32(sSinBarra[0]);
                    string nombre = sSinBarra[1];
                    string posicion = sSinBarra[2];
                    int nroOficina = Convert.ToInt32(sSinBarra[3]);
                    int piso = Convert.ToInt32(sSinBarra[4]);
                    string fechaIng = sSinBarra[5];

                    int dia = Convert.ToInt32(fechaIng.Substring(0, 2));
                    int mes = Convert.ToInt32(fechaIng.Substring(2, 2));
                    int año = Convert.ToInt32(fechaIng.Substring(4, 4));
                    string fechaIngreso = dia + "/" + mes + "/" + año;
                    fechaIng = fechaIngreso;

                    Empleados unEmpleado = new Empleados();

                    unEmpleado.NroEmpleado1 = nroEmp;
                    unEmpleado.Nombre1 = nombre.TrimEnd().TrimStart();
                    unEmpleado.Posicion = posicion.TrimEnd().TrimStart();
                    unEmpleado.NroOficina = nroOficina;
                    unEmpleado.Piso = piso;
                    unEmpleado.FechaIngreso = fechaIngreso;

                    emp.Add(unEmpleado);

                    emp.Sort(delegate(Empleados e1, Empleados e2)
                    {
                        return e1.NroEmpleado1.CompareTo(e2.NroEmpleado1);
                    });
                }

                foreach (Empleados em in emp)
                {
                    string empOrd = em.NroEmpleado1.ToString() + "\t" + em.Nombre1 + "\t" + em.Posicion + "\t" + em.NroOficina.ToString()
                        + "\t" + em.Piso.ToString() + "\t" + em.FechaIngreso;

                    lstLinesAfterChange.Items.Add(empOrd);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            //saveFileDialog1.ShowDialog();

            //if (saveFileDialog1.FileName != "")
            //{
            //    FileStream fs = (FileStream)saveFileDialog1.OpenFile();

            //    fs.Close();

            //}
            SerializeToXML(agen);

        }
    }
}

I have this, and i want to serialize to xml both lists :

List<Empleados> emp = new List<Empleados>();
List<Agenda> agen = new List<Agenda>();

I used SerializeToXML method that i found in other tutorial but when i run it an error shows up

"Error 1 Inconsistent accessibility: parameter type 'System.Collections.Generic.List' is less accessible than method 'AppPrueba.Form1.SerializeToXML(System.Collections.Generic.List)' C:\Users\722825\Desktop\Santi Cosas\AppPrueba\AppPrueba\Form1.cs 27 28 AppPrueba"

Thanks in advance if you can help me!

Abdelilah El Aissaoui
  • 4,204
  • 2
  • 27
  • 47
user2115574
  • 11
  • 1
  • 1
  • 2
  • 1
    You need to extract your problem into a smaller amount of code... We cannot read so much irrelevant code. – David S. Mar 18 '13 at 19:09
  • Use DataContractSerializer instead as described here: [http://stackoverflow.com/a/5941122/246622][1] [1]: http://stackoverflow.com/a/5941122/246622 – tomsv Mar 18 '13 at 19:11

3 Answers3

2

All the classes that you are trying to serialize to xml should be public. And for collection serialization you cannot use generics - either use untyped collections, like, ArrayList , or create a non-generic descendant class form List .

alex
  • 12,464
  • 3
  • 46
  • 67
  • It's allowed to serialize generics, why do you think it's not? – Dima Mar 19 '13 at 06:13
  • Turns out you can serialize generic lists. The trick I described is applied to generic dictionaries that cannot be serialized with default XmlSerializer. – alex Mar 19 '13 at 06:33
  • Are you sure it's true for .net 4? I'm looking on sources and Dictionary marked as [Serializable] and implements ISerializable, IDeserializationCallback. – Dima Mar 19 '13 at 06:48
  • [Serializable] controls binary serialization. Here is a post from the guy who cannot xml-serialize dictionary even in .net 4 - http://stackoverflow.com/questions/10830260/how-to-xml-serialize-and-deserialize-an-instance-of-dictionary-type-listactio – alex Mar 19 '13 at 06:55
  • 1
    Cheers! Actually, in this post they use 2.0 `[System.String, mscorlib, Version=2.0.0.0]`, but i tryed it with 4.0 and issue remains the same. – Dima Mar 19 '13 at 07:24
0

agen is private and SerializeToXML is public static. You need agen public

Dima
  • 6,721
  • 4
  • 24
  • 43
0

Your error says that the variabel List agen = new List(); needs to be public like this: public List agen = new List();

andreasnico
  • 1,478
  • 14
  • 23