.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

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.

Responses

No response found. Be the first to respond this post
Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend