0

How can I read the tag using linq2xml?

var q = (from c in xDocument.Descendants("colecciones").Descendants("BoTblPacientes")
                    select c).ToList(); 

but dont work.

<?xml version="1.0" ?>
<DalClassObject xmlns="http://www.asd.com">
  <objeto class="BoTblPacientes"></objeto>
  <validador>true</validador>
  <mensaje>El paciente  existe en el sistema .</mensaje>
  <colecciones>
    <BoTblPacientes>
      <tblpacientesmotivoconsulta>5</tblpacientesmotivoconsulta>
      <tblpacientestlfcasa>5</tblpacientestlfcasa>
      <tblpacientescelular>5</tblpacientescelular>
      <tblpacientesoficina>5</tblpacientesoficina>
      <tblpacientescorreo>5</tblpacientescorreo>
      <tblpacientesdireccion>5</tblpacientesdireccion>
      <tblpacientesapellidos>5</tblpacientesapellidos>
      <tblpacientesdocumento>5</tblpacientesdocumento>
      <tblpacientessexoid>0</tblpacientessexoid>
      <tblpacientesfechanacimiento class="sql-date">2012-05-13</tblpacientesfechanacimiento>
      <tblpacientesnombres>5</tblpacientesnombres>
      <tblpacientesid>2</tblpacientesid>
      <tblpacientesestadocivil>0</tblpacientesestadocivil>
      <tblpacientesfecharegistro class="sql-date">2012-05-13</tblpacientesfecharegistro>
      <tblpacienteidmaster>0</tblpacienteidmaster>
    </BoTblPacientes>
    <BoTblPacientes>
      <tblpacientesmotivoconsulta>23232</tblpacientesmotivoconsulta>
      <tblpacientestlfcasa>2332</tblpacientestlfcasa>
      <tblpacientescelular>23</tblpacientescelular>
      <tblpacientesoficina>23</tblpacientesoficina>
      <tblpacientescorreo>23</tblpacientescorreo>
      <tblpacientesdireccion>2323</tblpacientesdireccion>
      <tblpacientesapellidos>ewr</tblpacientesapellidos>
      <tblpacientesdocumento>5</tblpacientesdocumento>
      <tblpacientessexoid>0</tblpacientessexoid>
      <tblpacientesfechanacimiento class="sql-date">2012-03-29</tblpacientesfechanacimiento>
      <tblpacientesnombres>wer</tblpacientesnombres>
      <tblpacientesid>3</tblpacientesid>
      <tblpacientesestadocivil>0</tblpacientesestadocivil>
      <tblpacientesfecharegistro class="sql-date">2012-05-13</tblpacientesfecharegistro>
      <tblpacienteidmaster>0</tblpacienteidmaster>
    </BoTblPacientes>
  </colecciones>
</DalClassObject>
svick
  • 236,525
  • 50
  • 385
  • 514
NewCastle79
  • 73
  • 10

1 Answers1

1

Your initial “but doesn't work” statement isn't very helpful as you are not really clarifying what isn't working, but from the error i see, I am going to guess that your query isn't selecting anything.

Your xml root element declares a namespace but you are not specifying that namespace in the query, as a result nothing matches the string you are providing in the Descendants() method.

You need to look up by the local name instead

var q = (from c in xDocument.Descendants()
         where c.Name.LocalName == "BoTblPacientes"
         select c).ToList()

Just for completeness sake, as mentioned by OP in the comments, another option is to add the namespace to the query instead:

XNamespace ns = "http://www.asd.com";
var q = (from c in xDocument.Descendants(ns + "colecciones") 
         select c).ToList();
svick
  • 236,525
  • 50
  • 385
  • 514
psubsee2003
  • 8,563
  • 8
  • 61
  • 79
  • I get count = 0 this is the problem. – NewCastle79 May 20 '12 at 16:59
  • if I use XNamespace ns = "http://www.asd.com"; XDocument xDocument = XDocument.Load(txt); var q = (from c in xDocument.Descendants(ns + "colecciones") select c).ToList(); works, but if use the where clause dont work – NewCastle79 May 20 '12 at 17:06
  • Sorry, i was working without testing my code and just copied part of your initial query, I have editted my query, please try it now. – psubsee2003 May 20 '12 at 17:06
  • var q = (from c in xDocument.Descendants() where c.Name.LocalName == "BoTblPacientes" select c).ToList(); – NewCastle79 May 20 '12 at 17:09
  • @NewCastle79 your option with adding the namespace to the query also works and either is acceptable. That solution is preferred when you know the namespace and potentially could have multiple nodes with the same name in the same XML, but in different namespaces. I added that to my answer. My initial solution will work in cases where you don't necessarily know the namespace but will result in possibily too many matches if you have multiple name spaces. – psubsee2003 May 20 '12 at 17:25