XML query does not return values

1

The goal was to return the xml values but not returning anything:

using System;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Threading;


namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {

                StreamReader strm = new StreamReader("C:\Users\Visita\Desktop\test.xml");
                XDocument xd = XDocument.Load(strm);

                var consulta = from p in xd.Descendants("tb")
                               where p.Element("simbolo").Value == "H"
                               select new
                               {
                                   sa = Convert.ToString(p.Element("simbolo")),
                                   nm = Convert.ToString(p.Element("nAtomic"))
                               };

                foreach (var rs in consulta.ToArray())
                {
                    Console.WriteLine(Convert.ToString(rs.nm));
                } 
                  Console.Read();
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8" ?>
<Itens>
  <tb>
    <simbolo>G</simbolo>
    <nAtomic>56</nAtomic>
    <valencia>5</valencia>
  </tb>
  <tb>
    <simbolo>Ga</simbolo>
    <nAtomic>565</nAtomic>
    <valencia>55</valencia>
  </tb>

</Itens>

Is not returning any value, what is the error?

    
asked by anonymous 06.08.2014 / 14:41

1 answer

3

Remove where , because with where it will filter only <simbolo>H</simbolo> , and there is no record with tag .

where p.Element("simbolo").Value == "H"             

Looking like this:

StreamReader strm = new StreamReader("C:\Users\Visita\Desktop\test.xml");
XDocument xd = XDocument.Load(strm);

var consulta = from p in xd.Descendants("tb")
    select new
    {
        sa = Convert.ToString(p.Element("simbolo")),
        nm = Convert.ToString(p.Element("nAtomic"))
    };

    foreach (var rs in consulta.ToArray())
    {
        Console.WriteLine(Convert.ToString(rs.nm));
    } 
Console.Read();
    
06.08.2014 / 14:47