zoukankan      html  css  js  c++  java
  • 数据写入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);
        
       }
       }

      }
     }

  • 相关阅读:
    APICloud联合腾讯云推出“云主机解决方案“,各种福利等你拿
    WiFi模块Demo(新手教程)图文详解模块使用教程
    移动APP 微信支付完整过程(wxPay 方案一)
    APICloud开发者进阶之路 | txLive模块(直播类)试用分享
    解决R语言临时文件目录的问题(tempdir、tempfile)
    CentOS下SparkR安装部署:hadoop2.7.3+spark2.0.0+scale2.11.8+hive2.1.0
    Extending sparklyr to Compute Cost for K-means on YARN Cluster with Spark ML Library
    Running R jobs quickly on many machines(转)
    R语言快速深度学习进行回归预测(转)
    sparklyr包:实现Spark与R的接口
  • 原文地址:https://www.cnblogs.com/nosnowwolf/p/399436.html
Copyright © 2011-2022 走看看