在C#.Net中的现有XML文件中添加元素 [英] Add Elements in Existing XML File in C#.Net
问题描述
我使用C#.net创建XML文件. XML文件结构就是这样.
I create a XML file using C#.net. XML file structure is like this.
< pre>< Test>
<pre><Test>
< Software>
<Software>
< Code>是</Code>
<Code>Yes</Code>
</软件>
</Software>
</Test></pre>
</Test></pre>
在单击按钮时添加元素.因此,我在C#.net中创建了一个方法.
I want to add a element when click a button.So i create a method in C#.net.
</pre>私有静态void InsertNode(string xmlFilePath,string xmlNode,string innerText)
</pre>private static void InsertNode(string xmlFilePath,string xmlNode,string innerText)
{
尝试
{
XmlDocument xmlDoc = new XmlDocument();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
xmlDoc.Load(xmlFilePath);
XmlNode root = xmlDoc.DocumentElement;
XmlNode root = xmlDoc.DocumentElement;
XmlElement xmlEle = xmlDoc.CreateElement(xmlNode);
XmlElement xmlEle = xmlDoc.CreateElement(xmlNode);
xmlEle.InnerText = innerText;
xmlEle.InnerText = innerText;
root .InsertAfter(xmlEle,root.SelectSingleNode("/Software"));
root.InsertAfter(xmlEle, root.SelectSingleNode("/Software"));
xmlDoc.Save(xmlFilePath);
xmlDoc.Save(xmlFilePath);
MessageBox.Show(成功保存");
MessageBox.Show("Saved Successfully");
}
捕获(异常例外)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.Message);
}
}
私有无效按钮1_Click(对象发送者,EventArgs e)
private void button1_Click(object sender, EventArgs e)
{
InsertNode(xmlPath +"\\ Default.xml" ,"Function","true");
InsertNode(xmlPath + "\\Default.xml", "Function", "true");
}</pre>
}</pre>
但效果很好.我得到了输出
But it works fine . I got a output
< pre>< Test>
<pre><Test>
< Function> true</Function>
<Function>true</Function>
< Software>
<Software>
< Code>是</Code>
<Code>Yes</Code>
</Software>
</Software>
</Test></pre>
</Test></pre>
,但我想要这样
< pre><测试>
<pre><Test>
<软件>
<Software>
<代码>是< /Code>
<Code>Yes</Code>
< Function> true</Function>
<Function>true</Function>
</Software>
</Software>
</Test></pre>
</Test></pre>
那我该如何实现呢?请帮助我.
So how can i achieve this ? Please help me.
推荐答案
http://omegacoder.com/?p=46 [ ^ ]应该可以帮助您.
此 [ ^ ]也应该有用.
http://omegacoder.com/?p=46[^] should help you out.
This[^] should be useful as well.
这篇关于在C#.Net中的现有XML文件中添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!