Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
hi guys,

i want to add a new node in my existing "Input.XML" file. my xml file structure is like:

this is my Root node. i want when i click on button a new child node is add in this with a Element. a element name is coming from a textbox text. i use many code but when i click on different image its name come in text box that text is go and save in xml but it over write in my previous child node. i want when i click on button my child node is add again again with textbox text as an element. but my child node name is same.

for example:

<Images> \\ This is Root Node

<imgName> \\ This is my Child Node Which i want to add on btn click

Text1 \\ This is a element which is coming from a textbox.
</imgName> \\Child Node End

<%-------------When again i click on another image a new text is appear in textbox. when i click on button this text is also save in my xml file.



i hope you will understand what i want to say.. now please help me in this..

Updated 10-Jan-18 18:25pm
uspatel 2-Sep-11 1:10am    
What code u use on button click

Pleas used this code it will help
/file name
string filename = @"d:\temp\XMLFile2.xml";

//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();

//load from file

//create node and add value
XmlNode node = doc.CreateNode(XmlNodeType.Element, "Genre_Genre_Country", null);
//node.InnerText = "this is new node";

//create title node
XmlNode nodeTitle = doc.CreateElement("Title");
//add value for it
nodeTitle.InnerText = "This title is created by code";

//create Url node
XmlNode nodeUrl = doc.CreateElement("Url");
nodeUrl.InnerText = @"";

//add to parent node

//add to elements collection

//save back
Share this answer
adadsadsasd 2-Sep-11 1:22am    
string XmlFile = @"file:///C:\\Documents and Settings\\USITAjay\\My Documents\\Visual Studio 2008\\WebSites\\Presentation\\Input.xml";
XmlDocument xdoc = new XmlDocument();
XmlNode node = xdoc.CreateElement(XmlNodeType.Element,"imgName",null);
node.InnerText = txtHideImage.Text;

XmlNode nodetitle = xdoc.CreateElement("Images");
nodetitle.InnerText = "AJAY";



It gives a error :
1.The best overloaded method match for'System.Xml.XmlDocument.CreateElement(string, string, string)' has some invalid arguments
2.Argument '1': cannot convert from 'System.Xml.XmlNodeType' to 'string'
adadsadsasd 2-Sep-11 1:49am    
thanx a lot for that...
try Google!
one of the useful link may be this
try this add new node to existing xml file
Share this answer
adadsadsasd 2-Sep-11 1:32am    
I got this error on "URI formats are not supported." whats this?
The appendChild() method adds a child node to an existing node.

The new node is added (appended) after any existing child nodes.

Note: Use insertBefore() if the position of the node is important.

The following code fragment creates an element (), and adds it after the last child of the first element:



The insertBefore() method is used to insert a node before a specified child node.

This method is useful when the position of the added node is important:




check this link for more details[^]
Share this answer
Light weight XmlDocumentFragment can be good idea in this regards:
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900