日期:2014-04-05  浏览次数:20916 次

XmlDocument doc=new XmlDocument();
   doc.Load(Server.MapPath("registers.xml"));
   XmlNode root=doc.SelectSingleNode("/content");
   XmlElement xe=doc.CreateElement("item");
   XmlElement xeChild=doc.CreateElement("Id");
   xeChild.InnerText=id.Text.Trim();
   xe.AppendChild(xeChild);

   xeChild=doc.CreateElement("BlogName");
   xeChild.InnerText=blogName.Text.Trim();
   xe.AppendChild(xeChild);

   xeChild=doc.CreateElement("Address");
   xeChild.InnerText=address.Text.Trim();
   xe.AppendChild(xeChild);

   xeChild=doc.CreateElement("Server");
   xeChild.InnerText=serverName.Text.Trim();
   xe.AppendChild(xeChild);

   root.AppendChild(xe);
   root=doc.SelectSingleNode("/content/sum");
   int sum=Convert.ToInt32(root.InnerText)+1;
   root.InnerText=sum.ToString();
   doc.Save(Server.MapPath("registers.xml")); 

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<content>
  <sum>1</sum>
  <item>
    <Id>aa</Id>
    <BlogName>dd</BlogName>
    <Address>sd</Address>
    <Server>fdf</Server>
  </item>
</content>