views:

48

answers:

2
public Envio(int id)
{
    XDocument xml = XDocument.Parse(LoadFromService(id));
    ID = xml.Element("envio")
            .Element("de").Value;

    De = xml.Element("envio")
            .Element("de").Value;

    Para = xml.Element("envio")
            .Element("para").Value;

    Fecha = xml.Element("envio")
            .Element("fecha").Value;

    Descripcion = xml.Element("envio")
            .Element("descripcion").Value;
}



/*
    * <xml>
    *  <envio id="123">
    *      <de>Sergio</de>
    *      <para>Gabriela</para>
    *      <fecha>10/10/2010</fecha>
    *      <descripcion>Una moto de 30kg.</descripcion>
    *  </envio>
    * </xml>
    */

I want to extract every bit of information and also the ID attribute of the root tag,Envio.

Any help?

+1  A: 

Well , you don't seem to do anything with attributes (id).

Also; rather than .Value, cast is preferred as it will handle missing data by returning null.

SomeProp = (string)node.Element("foo");
Marc Gravell
+1  A: 

Your xml variable is an XDocument object that contains a single <xml> tag.

Therefore, xml.Element("envio") is null.

Instead, you need to write xml.Root.Element("envio").

SLaks