Collect data of a variable C #

1

Good morning. I have the following code that reads an xml, and collects the data and stores it in a variable.

private void leerxml(){
xElement rootnode= xelement.load(@"C:/xml.xml");
   foreach (xElement chlid in RootNode.Elements())
   {
       if(chield.Name.Localname.Equals("datos"))
       {
           string xml = child.Value.Tostring();
           Debug.Log(xml)
       }
   }
}

My Xml is this:

<libro> 
  <libroEjemplo> 
       <nombre>Alfredo Reino</nombre> 
       <email>[email protected]</email> 
  </libroEjemplo>

  <libroEjemplo> 
       <nombre>Alfredo Reino</nombre> 
       <email>[email protected]</email> 
  </libroEjemplo>  
</libro> 

In the xml variable, when I hit the Debug.log (xml) it takes this:

Alfredo Reino [email protected]
Alfredo Reino [email protected]

I need to take the data separately, so I can use it in another class, but I do not know how to take the data individually and then use it individually to show it in different places. If someone has any ideas .. Thanks in advance

    
asked by JuanPerez 28.03.2017 в 22:54
source

2 answers

1

I would advise you to use something like being

using System;
using System.Xml;
using System.Xml.Linq;
using System.Linq;


public class Program
{
    public static void Main()
    {
        string _xml = @"<libro> 
                          <libroEjemplo> 
                               <nombre>Alfredo Reino</nombre> 
                               <email>[email protected]</email> 
                          </libroEjemplo>
                          <libroEjemplo> 
                               <nombre>Alfredo Reino</nombre> 
                               <email>[email protected]</email> 
                          </libroEjemplo>  
                        </libro>";

        XElement xdocument = XElement.Parse(_xml);

        var list = from item in xdocument.Elements("libroEjemplo")
                  select new 
                  {
                     nombre = item.Element("nombre").Value,
                     email = item.Element("email").Value
                  };

        foreach(var item in list)
        {
            Console.WriteLine("{0}, {1}", item.nombre, item.email);
        }
    }
}

with the help of linq xml you can use the xml taking the values of each node

    
answered by 28.03.2017 в 23:40
0

I think you should first create a class that represents the values of the xml and then deserialize it to make it easier for you to work with the information. For the work with xml I use the visual studio tool (xsd.exe), very good that generates the class or the xsd (depending on what you need).

For transformations: Convert from c # to xsd: In visual studio console "C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v8.1A \ bin \ NETFX 4.5.1 Tools \ x64 \ xsd.exe" ClassLibrary1.dll / t: Customer

Convert from xsd to c #: in visual studio console "C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v8.1A \ bin \ NETFX 4.5.1 Tools \ x64 \ xsd.exe" / c / out: d: \ d: \ Schema.xsd

To generate the xsd I use the Altova suite (XMLSpy) and it takes me out of an xml example the xsd.

These steps are for work with large xml, for small, as it is in your case, you can directly do the class, it would be something like this:

    [Serializable]
    [XmlRoot("libro")]
    public class LibroXml
    {
        [XmlElement("libroEjemplo")]
        public List<LibroEjemplo> Libros { get; set; }
    }

    [Serializable]
    public class LibroEjemplo
    {
        [XmlElement("nombre")]
        public string Nombre { get; set; }

        [XmlElement("email")]
        public string Correo { get; set; }
    }

// Code for work with xml.

var obj = new LibroXml
{
    Libros = new List<LibroEjemplo>
    {
          new LibroEjemplo
          {
              Correo = "[email protected]",
              Nombre = "Yanet"
          },
          new LibroEjemplo
          {
               Correo = "[email protected]",
               Nombre = "Rafael"
          },
          new LibroEjemplo
          {
               Correo = "[email protected]",
               Nombre = "Alfredo Reino"
          }
     }
};    
XmlSerializer ser = new XmlSerializer(typeof(LibroXml));

//Para crear un fichero xml:
TextWriter writer = new StreamWriter("xmlExample.xml");
ser.Serialize(writer, obj);
writer.Close();
//Para leer de un fichero xml
TextReader reader = new StreamReader("xmlExample.xml");
var obj1 = ser.Deserialize(reader);
reader.Close();
    
answered by 30.03.2017 в 19:10