C#在特定结构中以XML格式添加节点 [英] C# Add a Node in XML in specific Structure
本文介绍了C#在特定结构中以XML格式添加节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hy Guys我的XML文件结构如下:
Hy Guys i have a XML File structured like this:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<TESTXML>
<Daten NAME ="Peter" DATUM="00" COMMENT="Blablabla" ID="1" />
<Daten NAME ="Klaus" DATUM="00" COMMENT="Blalabla" ID="2" />
<Daten NAME ="Thomas" DATUM="00" COMMENT="Blablala" ID="3" />
<Daten NAME ="Thomas" DATUM="00" COMMENT="Blablabla" ID="4" />
<Daten NAME ="Klaus" DATUM="00" COMMENT="Blablabla" ID="5" />
</TESTXML>
现在我只想添加一个类似于它的Node(Daten)到现有的XML文件
直到,我想通了一个:
now i just want to add a Node (Daten) similiar to it to the already existing XML File
Till yet,i figured out that one:
string filename = Server.MapPath("XMLFile.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, "Daten","Test");
//add to elements collection
doc.DocumentElement.AppendChild(node);
//save back
doc.Save(filename);
它添加一个节点,但不像上面的结构:(
任何人都可以提供帮助?Thx
It Adds a Node but not like the structure in Above :(
anyone can help? Thx
推荐答案
我想这会对你有所帮助
I think this would help you
string filename =Server.MapPath("XMLFile.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, "Daten", null);
XmlAttribute _NAME = doc.CreateAttribute("NAME");
_NAME.Value = "Test";
XmlAttribute _DATUM = doc.CreateAttribute("DATUM");
_DATUM.Value = "00";
XmlAttribute _COMMENT = doc.CreateAttribute("COMMENT");
_COMMENT.Value = "Blablabla";
XmlAttribute _ID = doc.CreateAttribute("ID");
_ID.Value = "6";
//add to elements collection
node.Attributes.Append(_NAME);
node.Attributes.Append(_DATUM);
node.Attributes.Append(_COMMENT);
node.Attributes.Append(_ID);
doc.DocumentElement.AppendChild(node);
//save back
doc.Save(filename);
这篇关于C#在特定结构中以XML格式添加节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文