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);
        
       }
       }

      }
     }

  • 相关阅读:
    Anaconda和Pycharm的安装和配置
    使用XAMPP集成开发环境安装Apache、PHP的配置说明
    新兴内存技术准备突围
    使嵌入式系统调试更容易:有用的硬件和软件提示
    保护嵌入式802.11 Wi-Fi设备时需要考虑的10件事
    关键任务应用程序依赖于故障保护存储器
    模拟内存计算如何解决边缘人工智能推理的功耗挑战
    如何为嵌入式应用选择适当的SSD
    Imec推出高性能芯片的低成本冷却解决方案
    交换机应用寻找10个完美的因素
  • 原文地址:https://www.cnblogs.com/nosnowwolf/p/399436.html
Copyright © 2011-2022 走看看