views:

60

answers:

2

Hi, I have this XML:

<?xml version="1.0" encoding="utf-8"?>

 <ConfiguraCanale ID_Comando="1">
  <canaleDigitalOUTPUT ID_Canale="1" >
      <stato>0</stato>
  </canaleDigitalOUTPUT>
 </ConfiguraCanale>


 <ConfiguraCanale ID_Comando="2">
  <canaleAnalogicoINPUT ID_Canale="2">    
   <timeAttesaPreCamp>00:03:00</timeAttesaPreCamp> 
  </canaleAnalogicoINPUT>
 </ConfiguraCanale>  

    </Comandi>

I must select the node canaleAnalogicoINPUT, and it's simple, but I must also take ID_Comando from ConfiguraCanale up to canaleAnalogicoINPUT.. because I select the node canaleAnalogicoINPUT I can't get attribute of the node ConfiguraCanale.

I use Linq to XML from few time...

Thanks!!!

A: 

It could be something like this:

var inputs = from e in doc.Descendants("canaleAnalogicoINPUT")
             select new
             {
                 CanaleAnalogicoINPUT = e, // extract what you need from this node
                 IDComando = int.Parse(e.Parent.Attribute("ID_Comando").Value)
             };
bruno conde
I resolve with ID_Comando = canale.Parent.Attribute("ID_Comando").ValueIn this case also FirstAttribute is good:ID_Comando = canale.Parent.FirstAttribute.Value, PARENT was the solution... I must study more! :(
malvin
A: 
var inputs = from e in doc.Elements("ConfiguraCanale")
             select new 
             {
                 IDComando = e.Attribute("ID_Comando").Value,
                 CanaleAnalogicoINPUT = e.Element("canaleAnalogicoINPUT")
             };
Thomas Levesque