how to get xml attribute values by its name in c# -
<entity> <fields> - <field name="expected"> <value>011919884841926</value> </field> - <field name="name"> <value>dial</value> </fields> <fields>.....</fields> <fields>.....</fields> <fields>.....</fields> <fields>......</fields> <fields>....</fields> </entity>
how dial = 011919884841926 key value pair in c#.
list<dictionary<string, string>> tempdefect = new list<dictionary<string, string>>(); if (!string.isnullorempty(responsexml)) { xmldocument configxml = new xmldocument(); configxml.loadxml(responsexml); xpathnavigator navigator = configxml.createnavigator(); xpathnodeiterator nodeiterator = (xpathnodeiterator)navigator.evaluate(rootentities + "/" + rootentity + "/" + rootelement); (int = 0; < nodeiterator.count; i++) { nodeiterator.movenext(); dictionary<string, string> tempdefectrows = new dictionary<string, string>(); configxml = new xmldocument(); configxml.loadxml(nodeiterator.current.outerxml.tostring()); navigator = configxml.createnavigator(); xpathnodeiterator nodeiterator1 = (xpathnodeiterator)navigator.evaluate(rootelement + "/" + loginparamselement); if (nodeiterator.count != 0) { (int j = 0; j < nodeiterator1.count; j++) { nodeiterator1.movenext(); xmlnode node = ((ihasxmlnode)nodeiterator1.current).getnode(); if (attributes.contains((t)enum.parse(typeof(t), node.attributes["name"].value.tostring().replace("-", "_"), true))) { if (node.haschildnodes) { configxml.loadxml(node.innerxml.tostring()); xpathnavigator navigator2 = configxml.createnavigator(); xpathnodeiterator nodeiterator2 = (xpathnodeiterator)navigator2.evaluate("value"); if (nodeiterator2.current.tostring().trim() != striptagschararray(nodeiterator2.current.tostring().replace(" ", "").trim()).trim()) { tempdefectrows.add(node.attributes["name"].value.tostring(), striptagschararray(nodeiterator2.current.tostring().replace(" ", "").trim()).trim()); } else { tempdefectrows.add(node.attributes["name"].value.tostring(), nodeiterator2.current.tostring().trim()); } } else { tempdefectrows.add(node.attributes["name"].value.tostring(), string.empty); } } } if (tempdefectrows.containsvalue(filterby)) { testid = tempdefectrows["id"].tostring(); tempdefect.add(tempdefectrows); if (typeof(t).name.tostring() == "test") break; } } } }
this research , work arround. im getting desired out put after 2 3 loops... thats reasson have posted here..... there easy way solution.
using linq xml
var xdoc = xdocument.load(filename); var dict = xdoc.descendants("fields") .todictionary(f => getvalue(f, "name"), f => getvalue(f, "expected")); string getvalue(xelement root, string attr) { return root.elements("field") .first(a => a.attribute("name").value == attr) .element("value").value; }
Comments
Post a Comment