XML Reading

Discussion in 'ASP .Net' started by André Freitas, Jan 13, 2010.

  1. I got a xml file like this:

    <menu>
    <categoria imagem="a">
    <item nome="b" />
    <item nome="c" />
    <item nome="d" />
    </categoria>
    <categoria imagem="e">
    <item nome="f" />
    <item nome="g" />
    </categoria>
    </menu>

    How can I read each of this values, like:

    a
    b
    c
    d

    f
    g
    h

    I dont know how many <category> i ll get, and how many <item> inside each
    <category>.

    Regards,
    André
    André Freitas, Jan 13, 2010
    #1
    1. Advertising

  2. André Freitas wrote:
    > I got a xml file like this:
    >
    > <menu>
    > <categoria imagem="a">
    > <item nome="b" />
    > <item nome="c" />
    > <item nome="d" />
    > </categoria>
    > <categoria imagem="e">
    > <item nome="f" />
    > <item nome="g" />
    > </categoria>
    > </menu>
    >
    > How can I read each of this values, like:
    >
    > a
    > b
    > c
    > d
    >
    > f
    > g
    > h
    >


    With .NET 3.5 you can use LINQ to XML
    (http://msdn.microsoft.com/en-us/library/bb387098.aspx), here is an example:

    XDocument doc = XDocument.Load("file.xml");
    var query = from cat in doc.Root.Elements("categoria")
    select new
    {
    imagem = (string)cat.Attribute("imagem"),
    names = from item in cat.Elements("item")
    select (string)item.Attribute("nome")
    };

    Here is how you could output the data:

    foreach (var cat in query)
    {
    Console.WriteLine("{0}:", cat.imagem);
    foreach (string name in cat.names)
    {
    Console.WriteLine("\t{0}", name);
    }
    Console.WriteLine();
    }

    --

    Martin Honnen --- MVP XML
    http://msmvps.com/blogs/martin_honnen/
    Martin Honnen, Jan 14, 2010
    #2
    1. Advertising

  3. thx so much

    "Martin Honnen" <> escreveu na notícia da
    mensagem:...
    > André Freitas wrote:
    >> I got a xml file like this:
    >>
    >> <menu>
    >> <categoria imagem="a">
    >> <item nome="b" />
    >> <item nome="c" />
    >> <item nome="d" />
    >> </categoria>
    >> <categoria imagem="e">
    >> <item nome="f" />
    >> <item nome="g" />
    >> </categoria>
    >> </menu>
    >>
    >> How can I read each of this values, like:
    >>
    >> a
    >> b
    >> c
    >> d
    >>
    >> f
    >> g
    >> h
    >>

    >
    > With .NET 3.5 you can use LINQ to XML
    > (http://msdn.microsoft.com/en-us/library/bb387098.aspx), here is an
    > example:
    >
    > XDocument doc = XDocument.Load("file.xml");
    > var query = from cat in doc.Root.Elements("categoria")
    > select new
    > {
    > imagem = (string)cat.Attribute("imagem"),
    > names = from item in cat.Elements("item")
    > select (string)item.Attribute("nome")
    > };
    >
    > Here is how you could output the data:
    >
    > foreach (var cat in query)
    > {
    > Console.WriteLine("{0}:", cat.imagem);
    > foreach (string name in cat.names)
    > {
    > Console.WriteLine("\t{0}", name);
    > }
    > Console.WriteLine();
    > }
    >
    > --
    >
    > Martin Honnen --- MVP XML
    > http://msmvps.com/blogs/martin_honnen/
    André Freitas, Jan 17, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page