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:
<images>
</images>

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.

<imgName>
Text2
</imgName>

</Images>

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

thnx
Posted
Updated 10-Jan-18 18:25pm
Comments
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
doc.Load(filename);

//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 = @"http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=659406&SiteID=1";

//add to parent node
node.AppendChild(nodeTitle);
node.AppendChild(nodeUrl);

//add to elements collection
doc.DocumentElement.AppendChild(node);

//save back
doc.Save(filename);
 
Share this answer
 
Comments
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();
xdoc.Load(XmlFile);
XmlNode node = xdoc.CreateElement(XmlNodeType.Element,"imgName",null);
node.InnerText = txtHideImage.Text;

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

node.AppendChild(nodetitle);
xdoc.DocumentElement.AppendChild(node);

xdoc.Save(XmlFile);

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
 
Comments
adadsadsasd 2-Sep-11 1:32am    
I got this error on xdoc.save(fileName). "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:
C#
xmlDoc=loadXMLDoc("books.xml");

newel=xmlDoc.createElement("edition");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);


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:
C#
xmlDoc=loadXMLDoc("books.xml");

newNode=xmlDoc.createElement("book");

x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book")[3];

x.insertBefore(newNode,y);


check this link for more details
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/e3f3c6b1-43ee-46d7-bc09-edb8dcedb8d1[^]
 
Share this answer
 
Light weight XmlDocumentFragment can be good idea in this regards:
https://msdn.microsoft.com/en-us/library/system.xml.xmldocumentfragment(v=vs.110).aspx
 
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