如何增加xml的Node
<?xml version="1.0" encoding="utf-8"?><Strudents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AllStudent>
<Student studentid="1">
<studentname>John Smith</studentname>
</Student>
<Student studentid="2">
<studentname>Bill Evjer</studentname>
</Student>
<Student studentid="3">
<studentname>Griesten Land</studentname>
</Student>
<Student studentid="4">
<studentname>Jack Lee</studentname>
</Student>
</AllStudent>
</Strudents>
这是我的Xml文件,我想要在AllStudent里面增加Student
效果如下
<?xml version="1.0" encoding="utf-8"?>
<Strudents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AllStudent>
<Student studentid="1">
<studentname>John Smith</studentname>
</Student>
<Student studentid="2">
<studentname>Bill Evjer</studentname>
</Student>
<Student studentid="3">
<studentname>Griesten Land</studentname>
</Student>
<Student studentid="4">
<studentname>Jack Lee</studentname>
</Student>
<Student studentid="21">
<studentname>Edson Chen</studentname>
</Student>
</AllStudent>
</Strudents>
但我写的却把
<Student studentid="21">
<studentname>Edson Chen</studentname>
</Student> 这条记录加到了</AllStudent>下面
我是这么写的
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("students.xml");
XmlNode root = xmlDoc.SelectSingleNode("Strudents");
XmlNode node = xmlDoc.SelectSingleNode("AllStudent");
XmlElement xe2 = xmlDoc.CreateElement("Student");
xe2.SetAttribute("studentid", "21");
XmlElement xesub1 = xmlDoc.CreateElement("studentname");
xesub1.InnerText = "Edson Chen";
xe2.AppendChild(xesub1);
root.AppendChild(xe2);
xmlDoc.Save("students.xml"); --------------------编程问答-------------------- 小弟跪求答案
orz.....
Orz..........
orZ...........
OrZ........
☺rz
¤rZ
●rz
╭︿︿︿╮
{/ o o /}
( (oo) )
︶ ︶︶
╭︿︿︿╮
{/-◎◎-/}
( (oo) )
︶︶︶
╭︿︿︿╮
{/ . . /}
( (oo) )
︶ ︶ ︶
╭︿︿︿╮
{/-★★-/}
( (oo) )
︶︶︶
╭︿︿︿╮
{/-●●-/}
( (oo) )
︶︶︶
╭︿︿︿╮
{/ - - /}
( (..) )
︶︶︶
╭︿︿︿╮
{/ ·· /}
( (00))
︶︶︶
╭⌒╮打雷啦━┅~ ¤ ╭⌒╮ ╭⌒╮
╭⌒╭⌒╮╭⌒╮~╭⌒╮︶︶, ︶︶
,︶︶︶︶,""︶~~ ,""~︶︶ ,""
* . :
\ | /
. ____ .
--- / \ --- .
. | ~ ~ | .
--- \__O_/ ---
... / \
|
::.--.-.::
:( ( )::::: 东边日出西边雨
(_, \ ) ,_):: 道是无晴却有情 |
:::-'--`--:::::::: ~~| , \ _ /
::::::::::::::::::: ,|`-._/| -== (_) ==-
::::::::^^::::::::.' | /||\ / \
::::::^^::::::::.' | ./ ||`\ |
:::::::::::::::/ `-. |/._ || \
::::::::::::::| || || \
~~=~_~^~ =~ \~~~~~~~'~~~~'~~~~/~~`` ~=~^~
~^^~~-=~^~ ^ `--------------'~^~=~^~_~^=~^~ --------------------编程问答-------------------- 急啊。。。。跪求。。。。。裸求。。。。。 --------------------编程问答-------------------- 算了。不求了。我知道答案了。。。增加试出来了。。。
(~ o ~)~zZ。。。。。。。。。
O(∩_∩)O哈哈~。。。。。。
;-)。。。。。
高兴。。。。。。。。。。。。。。。。
答案。。。。
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("students.xml");
XmlNode root = xmlDocument.SelectSingleNode("Students/AllStudent");
XmlElement xmlElement = xmlDocument.CreateElement("Student");
xmlElement.SetAttribute("studentid", "21");
XmlElement xmlElement2 = xmlDocument.CreateElement("studentname");
xmlElement2.InnerText = "Edson Chen";
xmlElement.AppendChild(xmlElement2);
root.AppendChild(xmlElement);
xmlDocument.Save("students.xml");
补充:.NET技术 , C#