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
Post a Comment