c # deserialize xml within a class

0

Hello, I have the following query. I have an Origin class, which has the following structure

    public class Origen
{
    public int IdOrigen { get; set; }
    public string Nombre { get; set; }
    public decimal Peso { get; set; }
    public bool Verdadero { get; set; }
    public DateTime Fecha { get; set; }
}

I fill this class with data as follows

            #region Llena la clase con datos
        Origen origen = new Origen();
        origen.IdOrigen = 1;
        origen.Nombre = "Nombre1";
        origen.Peso = 234;
        origen.Verdadero = true;
        origen.Fecha = DateTime.Now;
        #endregion

Then I serialize it with the following function

        private static string SerializarToXml(Object obj)
    {
        try
        {
            StringWriter strWriter = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            serializer.Serialize(strWriter, obj);
            string resultXml = strWriter.ToString();
            strWriter.Close();
            return resultXml;
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }

this generates an xml string without problems

<?xml version="1.0" encoding="UTF-16"?>

-<Origen xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<IdOrigen>1</IdOrigen>

<Nombre>Nombre1</Nombre>

<Peso>234</Peso>

<Verdadero>true</Verdadero>

<Fecha>2017-11-17T17:40:52.9402905-03:00</Fecha>

</Origen>

well ... to be able to rescue this data I use the following code

        XmlDocument xmlDatoRescatado = new XmlDocument();
        xmlDatoRescatado.LoadXml(xmlDatos);
        XmlNode nodoMain = xmlDatoRescatado.SelectSingleNode("Origen");
        int IdOrigen = Convert.ToInt32(nodoMain.SelectSingleNode("IdOrigen").LastChild.Value);
        string nombre = nodoMain.SelectSingleNode("Nombre").LastChild.Value;
        decimal peso = Convert.ToDecimal(nodoMain.SelectSingleNode("Peso").LastChild.Value);
        bool Verdadero = Convert.ToBoolean(nodoMain.SelectSingleNode("Verdadero").LastChild.Value);
        DateTime Fecha = Convert.ToDateTime(nodoMain.SelectSingleNode("Fecha").LastChild.Value);

but this method is not very effective for me, because the real class that serialized has many definitions, so I wanted to know, if there is a way to deserialize the xml, passing the mold of the class that I want to contain that data. in this case a copy of the Origin class.

Is this possible? or definitely the only method is the one I'm using so far? Greetings and thanks for your answers

    
asked by Luis Gabriel Fabres 17.11.2017 в 21:46
source

1 answer

1

The correct thing is that deserializar the xml to the class, as it is explained here

How to: Deserialize an object

it would be something like this

XmlSerializer serializer = new XmlSerializer(typeof(Origen));  

FileStream fileStream = new FileStream("ruta archivo xml", FileMode.Open);  

Origen origen = (Origen)serializer.Deserialize(fileStream);
    
answered by 17.11.2017 / 22:23
source