c# - Get value from node with same name -


i'd retrieve information xml file, way it's formatted pretty strange. here is...

<?xml version="1.0"?> <careers>     <careerlist>         <careername></careername>         <careerdescription></careerdescription>     </careerlist>     <careerlist>         <careername>cook</careername>         <careerdescription>cooks food people</careerdescription>     </careerlist> </careers> 

i'd 2nd value, cook , description cooks food people, instead i'm getting empty node. example...

    public string careerdescription(string careerfile)     {         xmldocument xmldoc = new xmldocument();         xmldoc.load(careerfile);         string description = xmldoc.selectsinglenode("careers/careerlist/careerdescription").innertext;         return description;     } 

how select second node instead of first?

you can use index in xpath expression:

xmldoc.selectsinglenode("careers/careerlist[2]/careerdescription").innertext 

personally i'd use linq xml instead, mind you:

var doc = xdocument.load(careerfile); return doc.root           .elements("careerlist")           .elementat(1) // 0-based           .element("careerdescription")           .value; 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -