Add Elements in Existing XML File in C#.Net
Posted By: Narayanan Posted Date: December 16, 2011
Points:5 Category :C#
|
| Hi friends, I create a XML file using C#.net. XML file structure is like this <Test> <Software> <Code>Yes</Code> </Software> </Test> I want to add a element when click a button.So i create a method in C#.net. private static void InsertNode(string xmlFilePath,string xmlNode,string innerText) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); XmlNode root = xmlDoc.DocumentElement; XmlElement xmlEle = xmlDoc.CreateElement(xmlNode); xmlEle.InnerText = innerText; root.InsertAfter(xmlEle, root.SelectSingleNode("/Software")); xmlDoc.Save(xmlFilePath); MessageBox.Show("Saved Successfully"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button1_Click(object sender, EventArgs e) { InsertNode(xmlPath + "\\Default.xml", "Function", "true"); }
But it works fine . I got a output <Test> <Function>true</Function> <Software> <Code>Yes</Code> </Software> </Test> but i want like this <Test>
<Software> <Code>Yes</Code> <Function>true</Function> </Software> </Test> So how can i achieve this ? Please help me.
|
|
You must Sign In To post reply
|