开发者

Will this XML parsing work?

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&开发者_JS百科gt;
    * </xml>
    */

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

Any help?


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");


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").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜