久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

asp.net 對XML的增刪改

 實(shí)力決定地位 2011-04-09

ASP.NET操作簡單的xml,增刪改查

溫習(xí)一下對xml的簡單操作,,貼出來收藏

xml文件格式:

 

<?xml version="1.0" encoding="utf-8"?>
<students>
  
<student>
    
<id>4</id>
    
<name>肖品4</name>
    
<age>21</age>
  
</student>
  
<student>
    
<id>5</id>
    
<name>肖品5</name>
    
<age>22</age>
  
</student>
  
<student>
    
<id>1</id>
    
<name>肖品1</name>
    
<age>18</age>
  
</student>
  
<student>
    
<id>2</id>
    
<name>肖品2</name>
    
<age>19</age>
  
</student>
  
<student>
    
<id>3</id>
    
<name>肖品3</name>
    
<age>20</age>
  
</student>
</students>

 

 

XMLtoDataSet.aspx 這個頁面包含了增刪改查

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.IO;

namespace XMLWEB
{
    
public partial class XMLtoDataSet : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                LoadGridView();
            }
        }

        
private void LoadGridView()
        {
            
//轉(zhuǎn)換一個XML文件(本地\網(wǎng)絡(luò)均可)為一個DataSet
            DataSet ds = new DataSet();
            
//ds = XMLDataSetConverter.ConvertXMLFileToDataSet(Server.MapPath("xmltodataset.xml"));
            
//ds = XMLDataSetConverter.ConvertXMLFileToDataSet("http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss");
            
//foreach (DataRow row in ds.Tables[0].Rows)
            
//{
            
//    string name = row["name"].ToString();
            
//    string age = row["age"].ToString();
            
//}

            StringReader sreader 
= null;
            XmlTextReader xtreader 
= null;
            
try
            {
                XmlDocument doc 
= new XmlDocument();
                doc.Load(Server.MapPath(
"xmltodataset.xml"));
                
                sreader 
= new StringReader(doc.InnerXml);
                xtreader 
= new XmlTextReader(sreader);
                ds.ReadXml(xtreader);
            }
            
catch (Exception)
            {

                
throw;

            }
            
finally
            {
                xtreader.Close();
                sreader.Close();
            }

            
this.GridView1.DataSource = ds.Tables[0];
            
this.GridView1.DataBind();
        }

        
protected void Button1_Click(object sender, EventArgs e)
        {
            
this.Add();
        }

        
protected void Button2_Click(object sender, EventArgs e)
        {
            
this.Update();
        }

        
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
if (e.CommandName == "edi")
            { 
                
//通過ID 獲取對象,,給控件賦值
                string[] entity = this.GetEntity(e.CommandArgument.ToString()).Split('|');
                
this.TextBox4.Text = entity[0];
                
this.TextBox5.Text = entity[1];
                
this.TextBox6.Text = entity[2];
            }
            
else if (e.CommandName == "del")
            { 
               
//通過ID刪除對象
                this.Delete(e.CommandArgument.ToString());
            }
        }


        
private void Add()
        {
            
string id = this.TextBox1.Text;
            
string name = this.TextBox2.Text;
            
string age = this.TextBox3.Text;

            
//在第一個前面插入一條信息
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(
"xmltodataset.xml"));
            XmlNode students 
= doc.SelectSingleNode("students");

            XmlElement student 
= doc.CreateElement("student");
            
//student.SetAttribute("id",id);

            XmlElement ele_id 
= doc.CreateElement("id");
            ele_id.InnerText 
= id;
            student.AppendChild(ele_id);

            XmlElement ele_name 
= doc.CreateElement("name");
            ele_name.InnerText 
= name;
            student.AppendChild(ele_name);

            XmlElement ele_age 
= doc.CreateElement("age");
            ele_age.InnerText 
= age;
            student.AppendChild(ele_age);

            students.InsertBefore(student,students.FirstChild);

            doc.Save(Server.MapPath(
"xmltodataset.xml"));

            
//重新加載GridView
            LoadGridView();

            
this.TextBox1.Text = "";
            
this.TextBox2.Text = "";
            
this.TextBox3.Text = "";
        }
        
private void Delete(string id)
        {
            
//通過ID獲取信息
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(
"xmltodataset.xml"));
            XmlNode students 
= doc.SelectSingleNode("students");
            
foreach (XmlNode student in students.ChildNodes)
            {
                XmlNode node_id 
= student.FirstChild;
                
if (node_id.InnerText == id)
                {
                    student.ParentNode.RemoveChild(student);
                }
            }
            doc.Save(Server.MapPath(
"xmltodataset.xml"));
            LoadGridView();
        }
        
private void Update()
        {
            
string id = this.TextBox4.Text;
            
string name = this.TextBox5.Text;
            
string age = this.TextBox6.Text;

            
//通過ID獲取信息,然后更改信息
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(
"xmltodataset.xml"));
            XmlNode students 
= doc.SelectSingleNode("students");
            
foreach (XmlNode student in students.ChildNodes)
            {
                XmlNode temp_node 
= student.FirstChild;
                
if (temp_node.InnerText == id)
                {
                    
//第一種方式
                    
//student.RemoveAll();
                    
//XmlElement ele_id = doc.CreateElement("id");
                    
//ele_id.InnerText = id;
                    
//student.AppendChild(ele_id);

                    
//XmlElement ele_name = doc.CreateElement("name");
                    
//ele_name.InnerText = name;
                    
//student.AppendChild(ele_name);

                    
//XmlElement ele_age = doc.CreateElement("age");
                    
//ele_age.InnerText = age;
                    
//student.AppendChild(ele_age);

                    
//第二種方式
                    student.ChildNodes[0].InnerText = id;
                    student.ChildNodes[
1].InnerText = name;
                    student.ChildNodes[
2].InnerText = age;

                    doc.Save(Server.MapPath(
"xmltodataset.xml"));
                    
break;
                }
            }

            LoadGridView();
        }
        
private string GetEntity(string id)
        {
            
string entity = "";
            XmlDocument doc 
= new XmlDocument();
            doc.Load(Server.MapPath(
"xmltodataset.xml"));
            XmlNode students 
= doc.SelectSingleNode("students");
           
            
foreach (XmlNode student in students.ChildNodes)
            {
                XmlNode temp_node 
= student.FirstChild;
                
if (temp_node.InnerText == id)
                {
                    
foreach (XmlNode item in student.ChildNodes)
                    {
                        entity 
+= item.InnerText + "|";
                    }
                    entity 
= entity.Substring(0,entity.Length -1);
                    
break;
                }
            }
            
return entity;
        }
    }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點(diǎn)擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多