views:

43

answers:

2

Team;

How are you doing? Im breaking my head trying to do this that seems simple but I can't figure it out... Suppose I have this XML as a string:

<calles>
  <calle>
   <nombre>CALLAO AV.</nombre>
   <altura>1500</altura>
   <longitud>-58.3918617027</longitud>
   <latitud>-34.5916734896</latitud>
   <barrio>Recoleta</barrio>
  </calle>
 </calles>

And and have this Type I created to map that XML:

public class Ubicacion
{
    public string Latitud { get; set; }
    public string Longitud { get; set; }
    public string Nombre { get; set; }
    public string Altura { get; set; }
    public string Barrio { get; set; }

    public Ubicacion() { }
}

I need to take that XML file and create an object with those values...

Does somebody know a quick way to do it? with C#? I have been trying this but is not working at all...

        XElement dir = XElement.Parse(text);

        Ubicacion informacion = from d in dir.Elements("calle").
                          select new Ubicacion
                          {
                              Longitud = d.Element("longitud").Value,
                              Latitud = d.Element("latitud").Value,
                              Altura = d.Element("altura").Value,
                              Nombre = d.Element("nombre").Value,
                              Barrio = d.Element("barrio").Value,
                          };
        return informacion.Cast<Ubicacion>();
    }

Any ideas?

Thanks!!!

+5  A: 

Have you tried this?

 XElement dir = XElement.Parse(text); 

        var informacion = from d in dir.Elements("calle"). 
                          select new Ubicacion 
                          { 
                              Longitud = d.Element("longitud").Value, 
                              Latitud = d.Element("latitud").Value, 
                              Altura = d.Element("altura").Value, 
                              Nombre = d.Element("nombre").Value, 
                              Barrio = d.Element("barrio").Value, 
                          }.ToList();

Remember that the LINQ query isn't executed until you iterate through it. The .ToList() extension method will do that for you. It will then produce a List<Ubicacion> collection.

Steve Brouillard
Shouldn't it be `List<Ubication> informacion = from...` ?
Gacek
Yes. I actually modified to var, which is what I would generally use in this context anyway.
Steve Brouillard
+2  A: 

One way of doing it would be this:

  • run your data.xml through xsd.exe twice - first using xsd.exe (filename).xml which will produce a XML schema for your XML file with the same name and a .xsd file extension
  • secondly, run xsd.exe /c (filename).xsd which will produce a C# class corresponding to your XML
  • now, you're able to easily deserialize the XML into a C# class
  • set up AutoMapper to easily map your generated/deserialized class into an instance of your target type - since both types have identical property names, setting up the AutoMapper is a breeze:

    Mapper.CreateMap<Deserialized, Ubicacion>();
    Ubicacion target = Mapper.Map<Deserialized, Ubicacion>(your-deserialized-instance);
    

    and that's all there is to do!

Depending on how many times you need to do this, that might be an easier way than manually parsing the XML into bits and pieces and assembling your new object instances based on that parsing.

marc_s