XML serialization/deserialization C#

Asked

Viewed 235 times

1

Good night!

I can’t use the data I do the XML deserialization, there is no exception or anything like that... I already checked the path, data but I can not list the data...

XML:

<?xml version="1.0" encoding="utf-8"?>
<Implement>
  <Locais>
    <Local>Tipo1</Local>
  </Locais>
  <TempoResposta>12</TempoResposta>
  <Auxilio>S</Auxilio>
</Implement>

Class:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;

namespace Liber.Util
{
    public class XMLConnectionString
    {
        [XmlRoot("Locais"), XmlType("Locais")]
        public class Locais
        {
            [XmlElement("Local")]
            public string Local { get; set; }
        }

        [XmlRoot("Implement"), XmlType("Implement")]
        public class Implement
        {
            [XmlElement("Locais")]
            public Locais Locais { get; set; }
            [XmlElement("TempoResposta")]
            public string TempoResposta { get; set; }
            [XmlElement("Auxilio")]
            public string Auxilio { get; set; }
        }

        public void desserializaConnectionString(string local)
        {
            FileInfo fi = new FileInfo(local);
            if (fi.Exists)
            {
                //ele entra aqui...

                FileStream fs = new FileStream(local, FileMode.Open);
                XmlRootAttribute xra = new XmlRootAttribute("Implement");
                XmlSerializer xs = new XmlSerializer(typeof(List<Implement>), xra);
                List<Implement> lista = (List<Implement>)xs.Deserialize(fs);
                fs.Close();

                foreach(Implement c in lista)
                {
                    MessageBox.Show(c.TempoResposta);
                }
            }
            else
            {

            }
        }
    }
}

By no means can I print on canvas through the:

Messagebox.Show(c.TempoResposta.Tostring())

Or Console.Writeline(c.TempoResposta.Tostring())

Or even access the object’s data.. Does anyone know what it can be ?

1 answer

0

Hello, to use a array element use XmlArray and for the element use XmlArrayItem

In your class:

public class XMLConnectionString
{
    [Serializable]
    [XmlRoot("Locais")]
    public class Locais
    {
        [XmlElement("Local")]
        public string Local { get; set; }
    }

    [Serializable]
    [XmlRoot("Implement")]
    public class Implement
    {
        [XmlElement("Locais")]
        public Locais Locais { get; set; }
        [XmlElement("TempoResposta")]
        public string TempoResposta { get; set; }
        [XmlElement("Auxilio")]
        public string Auxilio { get; set; }
    }


    [Serializable]
    [XmlRoot("ImplementCollection")]
    public class ImplementCollection
    {
        [XmlArray("Implements")]
        [XmlArrayItem("Implement", typeof(Implement))]
        public Implement[] list { get; set; }
    }

    public static void desserializaConnectionString(string local)
    {
        FileInfo fi = new FileInfo(local);
        if (fi.Exists)
        {
            ImplementCollection elements = null;
            XmlSerializer serializer = new XmlSerializer(typeof(ImplementCollection));
            StreamReader reader = new StreamReader(local);
            elements = (ImplementCollection)serializer.Deserialize(reader);
            reader.Close();
            Console.WriteLine(elements.list[0].TempoResposta);
        }
        else
        {
            MessageBox.Show("Arquivo não existe.");
        }
    }
}

In your XML:

<?xml version="1.0" encoding="utf-8"?> 
<ImplementCollection> 
    <Implements> 
        <Implement> 
            <Locais>
                <Local>Tipo1</Local>
            </Locais>
            <TempoResposta>12</TempoResposta>
            <Auxilio>S</Auxilio>
        </Implement> 
        <Implement> 
            <Locais>
                <Local>Tipo1</Local>
            </Locais>
            <TempoResposta>12</TempoResposta>
            <Auxilio>S</Auxilio>
        </Implement> 
     </Implements> 
</ImplementCollection>

The structure of XML became Classe->List->Items this because I created a class to store the items. if you need a list use elements.list.ToList();

To use:

// Diretório Debug da aplicação.
XMLConnectionString.desserializaConnectionString("teste.xml");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.