XmlDocument xd=new XmlDocument();
xd.Load("D:\\abc.xml");
XmlElement xe=xd.CreateElement("x");
xe.InnerText="wocao";
XmlElement xe1=xd.CreateElement("x1");
xe1.InnerText="wocao1";
xe.AppendChild(xe1);
XmlNode root = xd.SelectSingleNode("a");
root.AppendChild(xe);
xd.Save("D:\\abc.xml");
这里的root是xd是怎么关连起来了? 为什么改了root xd在保存会有变化 ?
xd.Load("D:\\abc.xml");
XmlElement xe=xd.CreateElement("x");
xe.InnerText="wocao";
XmlElement xe1=xd.CreateElement("x1");
xe1.InnerText="wocao1";
xe.AppendChild(xe1);
XmlNode root = xd.SelectSingleNode("a");
root.AppendChild(xe);
xd.Save("D:\\abc.xml");
这里的root是xd是怎么关连起来了? 为什么改了root xd在保存会有变化 ?
============================================
XmlDocument mydoc = new XmlDocument();
//加载指定的XML文件
mydoc.Load(severmap路径); //添加元素-姓名
XmlElement ele = mydoc.CreateElement("name");
XmlText text = mydoc.CreateTextNode(name);
//添加元素-邮箱
XmlElement ele1 = mydoc.CreateElement("mail");
XmlText text1 = mydoc.CreateTextNode(mail);
//添加元素-内容
XmlElement ele2 = mydoc.CreateElement("url");
XmlText text2 = mydoc.CreateTextNode(url);
//添加元素-网址
XmlElement ele3 = mydoc.CreateElement("msg");
XmlText text3 = mydoc.CreateTextNode(content); //添加文件的节点-msgrecord
XmlNode newElem = mydoc.CreateNode("element", "msgrecord", "");
//在节点中添加元素
newElem.AppendChild(ele);
newElem.LastChild.AppendChild(text);
newElem.AppendChild(ele1);
newElem.LastChild.AppendChild(text1);
newElem.AppendChild(ele2);
newElem.LastChild.AppendChild(text2);
newElem.AppendChild(ele3);
newElem.LastChild.AppendChild(text3);
//将节点添加到文档中
XmlElement root = mydoc.DocumentElement;
root.AppendChild(newElem);
//保存所有修改
mydoc.Save(FileName);