数据写入XML

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachPlat.templatecontrol;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient ;
using System.Configuration;
using System.IO;
using System.Xml;
namespace SystemControls
{
 /// <summary>
 /// Article 的摘要说明。
 /// </summary>
 public class Article:TemplatedWebControl
 {
  Button addArticleBtn;
  Button cancleBtn;
  TextBox txtTitle;
  TextBox txtContent;
  private DataSet myds;
  
  public Article()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
//  public void BindData()
//  {
//    
//  }
  protected override void AttachChildControls()
  {
   addArticleBtn=(Button)FindControl("addArticleBtn");
   this.addArticleBtn.Click+=new EventHandler(addArticleBtn_Click);
   cancleBtn=(Button)FindControl("cancleBtn");
   txtTitle=(TextBox)FindControl("txtTitle");
   txtContent=(TextBox)FindControl("txtContent");
   myds=new DataSet();
   string filepath=this.Page.Server.MapPath("~/XML/Article.XML");
   myds.ReadXml(filepath);
  
  }

  private void addArticleBtn_Click(object sender, EventArgs e)
  {
   if( txtTitle.Text.Trim( ) == string.Empty )
   {
    this.Page.Response.Write( "<script language = javascript>window.alert('用户名不能为空!')</script>" );
    return;
   }
   if( (myds.Tables[ 0 ].Select( "Title = '"+ txtTitle.Text.Trim( ) +"' ") ).Length == 0 )
   {
    XmlDocument doc = new XmlDocument();     
     
    //定义XML文档头文件
    XmlDeclaration dec = doc.CreateXmlDeclaration("1.0",null,null);
    doc.AppendChild(dec);
    XmlElement docRoot = doc.CreateElement("ArticleInfo");
    doc.AppendChild(docRoot);

    for(int i=0;i < myds.Tables[ 0 ].Rows.Count + 1; i++)
    {
     XmlNode Article = doc.CreateElement("Article" );
     docRoot.AppendChild(Article);
     XmlNode Title = doc.CreateElement( "Title" );
     XmlNode Content = doc.CreateElement("Content" );
     if( i <= myds.Tables[ 0 ].Rows.Count - 1 )
     {
      Title.InnerText = myds.Tables[ 0 ].Rows[ i ][ "Title" ].ToString( );
      Content.InnerText = myds.Tables[ 0 ].Rows[ i ][ "Content" ].ToString( );
     }
     else
     {
      Title.InnerText = txtTitle.Text;
      Content.InnerText = txtContent.Text;
     }
     Article.AppendChild(Title);
     Article.AppendChild(Content);
    }
        
    //保存XML文档
    string strPath = this.Page.Server.MapPath("~/XML/Article.XML");
    doc.Save(strPath);
    
   }
   }

  }
 }

 

你可能感兴趣的:(xml)