博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

E2生成xml文档[一]

Posted on 2006-03-16 22:39  ╁蓝驿┲→  阅读(144)  评论(0编辑  收藏  举报

using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace example_11
{
 /// <summary>
 /// _1 的摘要说明。
 /// </summary>
 public class _1 : Page
 {
  protected Button btnCreateXml;

  private void Page_Load(object sender, EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }

  #region Web 窗体设计器生成的代码

  protected override void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }

  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.btnCreateXml.Click += new EventHandler(this.btnCreateXml_Click);
   this.Load += new EventHandler(this.Page_Load);

  }

  #endregion

  private void btnCreateXml_Click(object sender, EventArgs e)
  {
   CreateXmlDocument();

  }

  private void CreateXmlDocument()
  {
   string filePath = Server.MapPath("Test1.xml");
   XmlTextWriter xw = new XmlTextWriter(filePath, Encoding.UTF8);
   xw.Formatting = Formatting.Indented;

   try
   {
    xw.WriteStartDocument(true);

    xw.WriteComment("使用XmlTextWriter创建XML文档");
    xw.WriteStartElement("手机信息");
    xw.WriteStartElement("手机");
    xw.WriteStartElement("品牌");
    xw.WriteAttributeString("型号", "6030");
    xw.WriteString("诺基亚");
    xw.WriteEndElement(); //品牌
    xw.WriteStartElement("部分技术参数");
    xw.WriteStartElement("兼容网络制式");
    xw.WriteElementString("网络制式", "GSM 900");
    xw.WriteElementString("网络制式", "GSM 1800");
    xw.WriteEndElement(); //兼容网络制式
    xw.WriteElementString("手机类型", "直板");
    xw.WriteStartElement("体积");
    xw.WriteElementString("长", "104mm");
    xw.WriteElementString("宽", "44mm");
    xw.WriteElementString("厚", "18mm");
    xw.WriteEndElement(); //体积
    xw.WriteElementString("重量", "90克");
    xw.WriteEndElement(); //部分技术参数
    xw.WriteElementString("售价", "1150.00元");

    xw.WriteEndElement(); //手机
    xw.WriteEndElement(); //手机信息
    xw.WriteEndDocument();//结束整个文档


    Response.Write("文档创建成功");

   }
   catch (Exception err)
   {
    Response.Write(err.ToString());
   }
   finally
   {
    xw.Close();
   }


  }
 }
}