XmlDocument 读取节点信息并修改

 

XmlDocument 读取节点信息并修改_第1张图片

Text.xml:


 


    1
    明*日
   
    25
    吉林省长春市
    5000
 

 
    2
    张*三
   
    28
    吉林省长春市
    3000
 

 
    3
    李*四
   
    23
    山西省长治市
    3000
 

 
    4
    aa
   
    26
    山西省长治市
    2000
 

 
    5
    测试
   
    18
    山东省日照市
    1500
 


htm:

   

   

       

           
               
                   
               
               
                   
               
               
                   
               
               
                   
               
           

                        修改XML文件

                                                    Font-Size="9pt" ForeColor="#333333" GridLines="None">
                           
                           
                               
                               
                               
                               
                               
                               
                           

                           
                           
                           
                           
                           
                       

                   

                         请选择要进行修改的节点:
                       

                   

                        新节点名:
                       

       

   
   

   


.cs:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet mydsRead = new DataSet();
            mydsRead.ReadXml(Server.MapPath("Test.xml"));
            GridView1.DataSource = mydsRead;
            GridView1.DataBind();
            DropDownList1.DataSource = mydsRead;
            DropDownList1.DataTextField = "EName";
            DropDownList1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {//思想:利用xmlDocument   加载xml->遍历修改->重新保存回xml

 
    1
    今日
   
    25
    吉林省长春市
    5000
 

//此例子为:1遍历NewDataSet根节点2再遍历Table节点下面的元素,取出元素EName,只修改EName
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("Test.xml"));//xmlDocument文档加载xml

//遍历节点 
        XmlNodeList xnl = doc.SelectSingleNode("NewDataSet").ChildNodes; ;//获取NewDataSet节点的所有子节点
        foreach (XmlNode xn in xnl)//遍历所有子节点
        {
            XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
            if (xe.Name == "Table")//判断节点名为Table  根节点下二级节点的名称
            {
                XmlNodeList xnlChild = xe.ChildNodes;//继续获取xe子节点的所有子节点
                foreach (XmlNode xnChild in xnlChild)//遍历
                {
                    XmlElement xeChild = (XmlElement)xnChild;//转换类型
                    if (xeChild.Name == "EName" && xeChild.InnerText == this.DropDownList1.SelectedValue.Trim())//元素名字及元素文本
                    {
                        xeChild.InnerText = TextBox1.Text.Trim();
                        Response.Write("");
                    }
                }
            }
        }
//重新保存回xml
        doc.Save(Server.MapPath("Test.xml"));
        Response.Write("");
    }

 

你可能感兴趣的:(textbox,table,asp,dataset,button,server,XML)