c# - Adding multiple Records to the XML file -
i add multiple records xml file , here code using,
xmltextwriter xwriter = new xmltextwriter("c:\\users\\desktop\\testfolder\\xdoc1.xml", encoding.utf8); xwriter.formatting = formatting.indented; xwriter.writestartelement("employee"); xwriter.writestartelement("person"); xwriter.writestartelement("name"); xwriter.writestring(textbox1.text); xwriter.writeendelement(); xwriter.writestartelement("designation"); xwriter.writestring(textbox2.text); xwriter.writeendelement(); xwriter.writestartelement("employee id"); xwriter.writestring(textbox3.text); xwriter.writeendelement(); xwriter.writestartelement("email"); xwriter.writestring(textbox4.text); xwriter.writeendelement(); xwriter.writeendelement(); xwriter.writeendelement(); xwriter.close();
the problem code 1 record can added. when try add 2nd record, previous record overwritten.
linq xml makes xml task easier. @ below code.
if (!system.io.file.exists("d:\\employees.xml")) { xelement element = new xelement("employees"); element.save("d:\\employees.xml"); } xelement doc = xelement.load("d:\\employees.xml"); xelement employee = new xelement("employees", new xelement("employee", new xelement("person", new xelement("name", textbox1.text), new xelement("designation", textbox2.text), new xelement("employeeid", textbox3.text), new xelement("email", textbox4.text)))); doc.add(employee); doc.save("d:\\employees.xml");
Comments
Post a Comment