ASP.NET操作简单的xml,增删改查

温习一下对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()
        {
            
//转换一个XML文件(本地\网络均可)为一个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;
        }
    }
}

 

 项目示例下载:XMLDemo.rar

posted @ 2010-12-30 16:37  PPDev  阅读(6208)  评论(4编辑  收藏  举报