XML to Type - Quick way?

Posted by MRFerocius on Stack Overflow See other posts from Stack Overflow or by MRFerocius
Published on 2010-04-16T16:52:27Z Indexed on 2010/04/16 18:33 UTC
Read the original article Hit count: 532

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!!!

© Stack Overflow or respective owner

Related posts about Xml

Related posts about LINQ