比如要构造这样的字符串:
<OnlineEdu>
<head>
<PassWord>****</PassWord>
<ServiceCode>BS002</ServiceCode>
</head>
<body>
<PeopleList>
<PeopleInfo>
<Idcard>3203231993052802**</Idcard>
<AptitudeCode>0001|000101|00010102</AptitudeCode>
<TrainType>初领</TrainType>
</PeopleInfo >
<PeopleInfo>
<Idcard>3203231993052802**</Idcard>
<AptitudeCode>0001|000101|00010103</ AptitudeCode>
<TrainType>复审</ TrainType>
</PeopleInfo >
</PeopleList>
</body>
</OnlineEdu>
这个字符串 前面 "head"的部分是固定的。我们要添加的就是Peopleinfo部分;
string xml=string.Empty; //定义一个空字符串
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<OnlineEdu>"
+ "<head>"
+ "<PassWord>*****</PassWord>"
+ "<ServiceCode>BS002</ServiceCode>"
+ "</head>"
+ "<body>"
+ "<PeopleList>"
+ "</PeopleList>"
+ "</body>"
+ "</OnlineEdu>"; //把一些固定的先写好
接着选择要插入的父节点位置:这里是“Popelelist”
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode Peoplelist = doc.SelectSingleNode("OnlineEdu/body/PeopleList");
接下来生成“Peopleinfo”节点信息
XmlElement peopleinfo = doc.CreateElement("PeopleInfo");
XmlElement Idcard = doc.CreateElement("Idcard");
XmlElement AptitudeCode = doc.CreateElement("AptitudeCode");
XmlElement TrainType = doc.CreateElement("TrainType");
Idcard.InnerText = "123124";
AptitudeCode.InnerText = "001|001001";
TrainType.InnerText = "初领";
peopleinfo.AppendChild(Idcard);
peopleinfo.AppendChild(AptitudeCode);
peopleinfo.AppendChild(TrainType);
再把“Peopleinfo”插入到 “Peoplelist”
Peoplelist.AppendChild(peopleinfo);
这样就能完成插入了。