Wednesday, 15 July 2015

c# - Close XDocument instance -



c# - Close XDocument instance -

i have been doing research on still couldn't prepare it. have function:

private void addnewservice() { string strpath = "servicestoexecute.xml"; string strservicename = tbnewservice.text; //try //{ xdocument xddocument; using (xmlreader xmlreader = xmlreader.create(strpath)) { xddocument = xdocument.load(xmlreader); xelement root = new xelement("service"); root.add(new xelement("name", strservicename)); xddocument.element("servicestoexecute").add(root); xmlreader.close(); xddocument.save(strpath); }

and error while trying save file... idea? think missing really, stupid can't see now.

i believe you're opening file named servicestoexecute.xml within xmlreader , trying save xdocument @ same path... aka trying overwrite file have open reading. per @mikez above, should simplify code using xdocument.load(string path) overload such:

private void addnewservice() { string strpath = "servicestoexecute.xml"; string strservicename = tbnewservice.text; //try //{ xdocument xddocument = xdocument.load(strpath); xelement root = new xelement("service"); root.add(new xelement("name", strservicename)); xddocument.element("servicestoexecute").add(root); // save xddocument.save(strpath);

c# xml linq-to-xml xmlreader

No comments:

Post a Comment