0

I've been trying to make this query work. I have an XML that looks like this:

<Usuarios>
       <Usuario>
        <username>ghamilton</username>
        <nombre>Catherine Hart</nombre>
        <fechaNacimiento>12-03-1983 00:00:00</fechaNacimiento>
        <nickname>ghamilton</nickname>
        <EventosInscripto/>
        <Amistades>
            <amistad>
                <username>dreed</username>
                <nombre>Ruby Price</nombre>
                <tipo>Aceptado</tipo>
                <nickname>dreed</nickname>
                <fechaNacimiento>23-12-1986 00:00:00</fechaNacimiento>
            </amistad>
            <amistad>
                <username>ffernandez</username>
                <nombre>Michael Rivera</nombre>
                <tipo>Aceptado</tipo>
                <nickname>ffernandez</nickname>
                <fechaNacimiento>07-02-1997 00:00:00</fechaNacimiento>
            </amistad>
        </Amistades>
    </Usuario>
    <!-- a lot more 'Usuario' tags after this one -->
<Usuarios/>

What i want to do is get all the Usuarios with the values stored in the tags 'username','nombre','fechaNacimiento', and nickname.

For that I have this code:

 XmlNodeList usuarios = xmlDoc.SelectNodes("/Usuarios/Usuario");
            foreach (XmlNode usuario in usuarios)
            {
               //I would use this object to populate a treeview
                rg = new Registrado(
                    xmlNode.SelectSingleNode(@"/username").InnerText,
                    "NA",
                    xmlNode.SelectSingleNode(@"/nombre").InnerText,
                    DateTime.ParseExact(xmlNode.SelectSingleNode(@"/fechaNacimiento").InnerText, "dd-MM-yyyy HH:mm:ss", null),
                    xmlNode.SelectSingleNode(@"/nickname").InnerText,
                    null,
                    null,
                    null,
                    null,
                    true
                    );
             }

But this xPath always selects the first 'usuario' and it's children and doesn't loop through them. It does, actually, loop through the whole document giving the exact ammount of matches, but always with the same data returned.

Any ideas on why my XPath is failing this bad?

Thanks!

driden
  • 17
  • 1
  • 5
  • I believe your code should be failing as you trying to select nodes starting from the root (like "/username") and hence you should be getting `NullReference` exceptions... – Alexei Levenkov Mar 05 '14 at 17:28

1 Answers1

2

you should be using usuario instead of xmlNode in the Registrado function i.e.

XmlNodeList usuarios = xmlDoc.SelectNodes("Usuarios/Usuario");
foreach (XmlNode usuario in usuarios)
{
   var username = usuario.SelectSingleNode("username").InnerText;
   var nombre = usuario.SelectSingleNode("nombre").InnerText;
   var fechaNacimiento = usuario.SelectSingleNode("fechaNacimiento").InnerText 
   var nickName = usuario.SelectSingleNode("nickname").InnerText;
   var datefechaNacimiento = DateTime.ParseExact(fechaNacimiento, 
                              "dd-MM-yyyy HH:mm:ss", null);

   rg = new Registrado(username, "NA",nombre, datefechaNacimiento,nickName,
                    null, null,null,null,true);
}

also on a very important note, always ensure a null check whenever dealing with selecting nodes i.e. instead of directly accessing the nodes innerText, first ensure they are not null i.e.

if(usuario.SelectSingleNode("username")!=null)
{
   strUsername = usuario.SelectSingleNode("username").InnerText;
}

also, though XmlDocument class if very powerful and useful, also look at Linq-to-XML

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59