this question has answer here:
- how can single node data using linq 5 answers
i'd make update xml node data, have following xml node
<categories> <category> <id>1</id> <name>computer</name> <description>information tech.</description> <active>false</active> </category> <category> <id>2</id> <name>cate1</name> <description>mmukh</description> <active>true</active> </category> </categories> for example category id=1, , fetch data textboxes. how can save changes node.
thanks alot.
try solution,
string xml = @"<categories> <category> <id>1</id> <name>computer</name> <description>information tech.</description> <active>false</active> </category> <category> <id>2</id> <name>cate1</name> <description>mmukh</description> <active>true</active> </category> </categories>"; xdocument xdoc = xdocument.parse(xml); int id = 1; var items = xdoc.xpathselectelement("//category[id=" + id + "]") .elements() .todictionary(e => e.name.localname, e => (string)e); if (items != null) { // display these fields text box console.writeline(items["name"]); console.writeline(items["description"]); console.writeline(items["active"]); } you can use above code display data in textbox. , below code update existing xml updated data.
// updated value xml element - description string description = "computer 123"; var items1 = item in xdoc.descendants("category") item.element("id").value == id.tostring() select item; foreach (xelement itemelement in items1) { itemelement.setelementvalue("name", description); } xdoc.save("data.xml"); you can updated xml data in xdoc.
Comments
Post a Comment