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

E4_xml文档创建[二]

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

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

namespace example_11
{
 /// <summary>
 /// _2 的摘要说明。
 /// </summary>
 public class _2 : Page
 {
  protected Label Label1;
  protected Label Label2;
  protected Label Label3;
  protected Label Label4;
  protected Label Label5;
  protected Label Label6;
  protected Label Label8;
  protected Label Label9;
  protected Button btnSubmit;
  protected Button btnReset;
  protected TextBox txtBrand;
  protected CheckBoxList cblNetwork;
  protected DropDownList dplType;
  protected TextBox txtLength;
  protected TextBox txtWidth;
  protected TextBox txtThickness;
  protected TextBox txtWeight;
  protected TextBox txtPrice;
  protected Label Label10;
  protected Label Label11;
  protected Label Label12;
  protected Label Label13;
  protected Label Label14;
  protected TextBox txtModel;
  protected Label Label7;

  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.btnReset.Click += new System.EventHandler(this.btnReset_Click);
   this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }

  #endregion

  private void btnSubmit_Click(object sender, EventArgs e)
  {
   CreateMobilePhoneInfoXmlDocument();
  }

  private void CreateMobilePhoneInfoXmlDocument()
  {
   string filePath = Server.MapPath("手机信息.xml");
   XmlTextWriter xw = new XmlTextWriter(filePath, Encoding.UTF8);
   xw.Formatting = Formatting.Indented;

   try
   {
    string brand = txtBrand.Text.Trim();

    string phoneModel = txtModel.Text.Trim(); //获取品牌
    string phoneType = dplType.SelectedValue; //获取型号
    int length = Convert.ToInt32(txtLength.Text); //获取长度
    int width = Convert.ToInt32(txtWidth.Text); //获取宽度
    int thickness = Convert.ToInt32(txtWidth.Text); //获取厚度
    int weight = Convert.ToInt32(txtWeight.Text); //获取重量
    float price = Convert.ToSingle(txtPrice.Text); //获取售价


    xw.WriteStartDocument(true); //写声明

    xw.WriteComment("使用XmlTextWriter创建XML文档"); //写注释
    xw.WriteStartElement("手机信息"); //“手机信息”起始标记
    xw.WriteStartElement("手机"); //“手机”起始标记
    xw.WriteStartElement("品牌"); //“品牌”起始标记
    xw.WriteAttributeString("型号", phoneModel); //“型号”属性
    xw.WriteString(brand); //“品牌”的值
    xw.WriteEndElement(); //“品牌”结束标记
    xw.WriteStartElement("部分技术参数"); //“部分技术参数”起始标记
    xw.WriteStartElement("兼容网络制式"); //“兼容网络制式”起始标记

    for (int i = 0; i < cblNetwork.Items.Count; i++)
    {
     if (cblNetwork.Items[i].Selected)
     {
      xw.WriteElementString("网络制式", cblNetwork.Items[i].Value);
     }
    } //遍历所有的网络制式,若打上了钩,则写入XML文档

    xw.WriteEndElement(); //“兼容网络制式”结束标记
    xw.WriteElementString("手机类型", phoneType); //将选择的“手机类型”值写入
    xw.WriteStartElement("体积"); //“体积”起始标记
    xw.WriteElementString("长", length.ToString() + "mm"); //写入长度
    xw.WriteElementString("宽", width.ToString() + "mm"); //写入宽度
    xw.WriteElementString("厚", thickness.ToString() + "mm"); //写入厚度
    xw.WriteEndElement(); //“体积”结束标记
    xw.WriteElementString("重量", weight.ToString() + "克"); //写入重量
    xw.WriteEndElement(); //“部分技术参数”结束标记
    xw.WriteElementString("售价", price.ToString() + "元"); //写入售价

    xw.WriteEndElement(); //“手机”结束标记
    xw.WriteEndElement(); //“手机信息”结束标记
    xw.WriteEndDocument();

    Response.Write("<font color=red>XML文档创建成功</font>");


   }
   catch (Exception err)
   {
    Response.Write(err.ToString());
   }
   finally
   {
    xw.Close();//关闭XmlTextWriter


   }


  }

  private void btnReset_Click(object sender, System.EventArgs e)
  {
   Response.Redirect("2.aspx");
  }


 }
}