• 数据写入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);
        
       }
       }

      }
     }

  • 相关阅读:
    php数组函数array_slice、array_splice
    php使用curl_init()和curl_multi_init()多线程的速度比较详解
    mysql忘记root密码
    php的RSA非对称加密
    phpstudy开启xdebug
    Centos7系统yum安装软件 No more mirrors to try
    python数据分析与数据可视化入门级
    第一周博客总结
    python——pandas初体验
    第十六周博客总结
  • 原文地址:https://www.cnblogs.com/nosnowwolf/p/399436.html
Copyright © 2020-2023  润新知