參考
xmldoc = new XmlDocument ( ) ;
//加入XML的聲明段落,<?xml version="1.0" encoding="gb2312"?>
XmlDeclaration xmldecl;
xmldecl = xmldoc.CreateXmlDeclaration("1.0","gb2312",null);
xmldoc.AppendChild ( xmldecl);
//加入一個(gè)根元素
xmlelem = xmldoc.CreateElement ( "" , "Employees" , "" ) ;
xmldoc.AppendChild ( xmlelem ) ;
//加入另外一個(gè)元素
for(int i=1;i<3;i++)
{
XmlNode root=xmldoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1=xmldoc.CreateElement("Node");//創(chuàng )建一個(gè)<Node>節點(diǎn)
xe1.SetAttribute("genre","李贊紅");//設置該節點(diǎn)genre屬性
xe1.SetAttribute("ISBN","2-3631-4");//設置該節點(diǎn)ISBN屬性
XmlElement xesub1=xmldoc.CreateElement("title");
xesub1.InnerText="CS從入門(mén)到精通";//設置文本節點(diǎn)
xe1.AppendChild(xesub1);//添加到<Node>節點(diǎn)中
XmlElement xesub2=xmldoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmldoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<Employees>節點(diǎn)中
}
//保存創(chuàng )建好的XML文檔
xmldoc.Save ( Server.MapPath("data.xml") ) ;
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。