在现有xml文件中添加新节点 [英] add a new node in existing xml file
问题描述
大家好,
我想在现有的Input.XML文件中添加一个新节点。我的xml文件结构如下:
< images>
< / images>
这是我的根节点。我想当我点击按钮时,一个新的子节点将添加一个元素。元素名称来自文本框文本。我使用了很多代码,但是当我点击不同的图像时,它的名字会出现在文本框中,文本将以xml格式保存,但它会在我之前的子节点中写入。我希望当我点击按钮时,我的子节点再次添加,文本框文本作为元素。但我的子节点名称相同。
例如:
< Images> \\这是根节点
< imgName> \\这是我的孩子节点我想在btn上添加点击
Text1 \\这是一个来自文本框的元素。 br />
< / imgName> \\Child Node End
<%-------------当我再次点击另一张图片时,新文字是出现在文本框中。当我点击按钮时,这个文本也保存在我的xml文件中。
< imgName>
Text2
< / imgName>
< / Images>
i希望你能理解我的意思想说..现在请帮帮我..
thnx
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
推荐答案
请用过这个代码它会帮助
/文件名
string filename = @d:\ temp \ XMLFile2.xml;
//创建XmlDocument的新实例
XmlDocument doc = new XmlDocument();
//从文件加载
doc.Load(filename);
//创建节点并添加值
XmlNode node = doc .CreateNode(XmlNodeType.Element,Genre_Genre_Country,null);
//node.InnerText =这是新节点;
//创建标题节点
XmlNode nodeTitle = doc.CreateElement(Title);
//为它增加值
n odeTitle.InnerText =此标题由代码创建;
//创建Url节点
XmlNode nodeUrl = doc.CreateElement(网址);
nodeUrl.InnerText = @http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=659406&SiteID=1;
//添加到父节点
node.AppendChild(nodeTitle);
node.AppendChild(nodeUrl);
//添加到元素集合
doc.DocumentElement.AppendChild(node);
//保存回来
doc.Save(文件名);
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);
试试Google!
其中一个有用的链接可能是这个
试试这个添加新节点到现有的xml文件
appendChild()方法将子节点添加到现有节点。
在任何现有子节点之后添加(附加)新节点。
注意:如果节点的位置很重要,请使用insertBefore()。
以下代码片段创建一个元素(),并且在第一个元素的最后一个子元素之后添加它:
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:
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
insertBefore()方法用于在指定的子节点之前插入节点。
当添加的节点的位置很重要时,此方法很有用:
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:
xmlDoc=loadXMLDoc("books.xml");
newNode=xmlDoc.createElement("book");
x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book")[3];
x.insertBefore(newNode,y);
查看此链接了解更多详情
http://social.msdn。 microsoft.com/forums/en-US/csharpgeneral/thread/e3f3c6b1-43ee-46d7-bc09-edb8dcedb8d1 [ ^ ]
这篇关于在现有xml文件中添加新节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!