Get Value by Name of the same Element in XML using C# -


i'm having following xml, in i'm having 2 nodes namely "param" names different. need value name node param. kindly @ code

void main() {     string _commentxml = string.format("<root>{0}{1}</root>"                              , "<param name=\"super\">parameter one</param>"                              , "<param name=\"supreme\">parameter two</param>");      xmldocument _comment = new xmldocument();     _comment.loadxml(_commentxml);       xelement element = xelement.load(_comment.documentelement.createnavigator().readsubtree());     trygetelementvalue(element, "param").dump(); }  public string trygetelementvalue(xelement parentel, string elementname, string defaultvalue = null) {     var foundel = parentel.element(elementname);      if (foundel != null)     {         var xyz = foundel.elements("param");          if (xyz != null)         {             return xyz.first(x => x.attribute("name").value == "super").value;         }     }      return defaultvalue; } 

i can't able value of param name=super

i refereed 1 of stack-overflow question opt requirement can't.

referred: xdocument xml element value of name attribute

kindly assist me.

why mess?

xdocument has descendants method , linq it's easy:

var xdoc = xdocument.parse(_commentxml); var xel = xdoc.descendants("param")               .where(xelement => xelement.attribute("name")?.value == "super"); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -