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

//建一個新的空的XML文檔
   XmlTextWriter objXml = new XmlTextWriter(Server.MapPath("Text.xml"),null);
   //格式化輸出XML文檔
   objXml.Formatting = Formatting.Indented;
   objXml.Indentation = 4;
   //寫入XML文檔標記
   objXml.WriteStartDocument();
   //寫入XML文檔注釋
   objXml.WriteComment("Created using an XML" + Context.Timestamp);
   //寫入根元素
   objXml.WriteStartElement("BookList");
   //寫入元素
   objXml.WriteStartElement("Book");
   //寫入屬性
   objXml.WriteAttributeString("Category","Technology");
   //寫入屬性值
   objXml.WriteAttributeString("PageCount","1234");
   //寫入子元素及文本值
   objXml.WriteElementString("Title","Professional Video Recorder Programming");
   //寫入子元素及文本值
   objXml.WriteElementString("ReleaseDate","02/02/2002");
   //寫入元素
   objXml.WriteStartElement("AuthorList");
   //寫入子元素及文本值
   objXml.WriteElementString("Author","Framcesca Unix");
   //寫入子元素及文本值
   objXml.WriteElementString("Author","william Soft");
   //關閉子元素、元素、根元素
   objXml.WriteEndElement();
   objXml.WriteEndElement();
   objXml.WriteEndElement();
   //清除緩存
   objXml.Flush();
   //關閉對像
   objXml.Close();