XmlDocument 读取节点信息并修改

 

Text.xml:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <EmployeeID>1</EmployeeID>
    <EName>明*日</EName>
    <ESex>男</ESex>
    <EAge>25</EAge>
    <EPlace>吉林省长春市</EPlace>
    <EMoney>5000</EMoney>
  </Table>
  <Table>
    <EmployeeID>2</EmployeeID>
    <EName>张*三</EName>
    <ESex>男</ESex>
    <EAge>28</EAge>
    <EPlace>吉林省长春市</EPlace>
    <EMoney>3000</EMoney>
  </Table>
  <Table>
    <EmployeeID>3</EmployeeID>
    <EName>李*四</EName>
    <ESex>女</ESex>
    <EAge>23</EAge>
    <EPlace>山西省长治市</EPlace>
    <EMoney>3000</EMoney>
  </Table>
  <Table>
    <EmployeeID>4</EmployeeID>
    <EName>aa</EName>
    <ESex>女</ESex>
    <EAge>26</EAge>
    <EPlace>山西省长治市</EPlace>
    <EMoney>2000</EMoney>
  </Table>
  <Table>
    <EmployeeID>5</EmployeeID>
    <EName>测试</EName>
    <ESex>男</ESex>
    <EAge>18</EAge>
    <EPlace>山东省日照市</EPlace>
    <EMoney>1500</EMoney>
  </Table>
</NewDataSet>
htm:
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <table align="center" border="1" cellpadding="0" cellspacing="0">
                <tr>
                    <td style="font-size: 9pt; color: #ff0000; text-align: center">
                        修改XML文件</td>
                </tr>
                <tr>
                    <td style="text-align: center">
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                            Font-Size="9pt" ForeColor="#333333" GridLines="None">
                            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                            <Columns>
                                <asp:BoundField DataField="EmployeeID" HeaderText="员工ID" ReadOnly="True" />
                                <asp:BoundField DataField="EName" HeaderText="姓名" />
                                <asp:BoundField DataField="ESex" HeaderText="性别" />
                                <asp:BoundField DataField="EAge" HeaderText="年龄" />
                                <asp:BoundField DataField="EPlace" HeaderText="籍贯" />
                                <asp:BoundField DataField="EMoney" HeaderText="工资" />
                            </Columns>
                            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                            <AlternatingRowStyle BackColor="White" />
                        </asp:GridView>
                    </td>
                </tr>
                <tr>
                    <td style="font-size: 9pt; text-align: left">
                        &nbsp;请选择要进行修改的节点:<asp:DropDownList ID="DropDownList1" runat="server">
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td style="font-size: 9pt; text-align: center">
                        新节点名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="修改" /></td>
                </tr>
            </table>
        </div>
   
    </div>
    </form>
</body>
.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
<NewDataSet>
  <Table>
    <EmployeeID>1</EmployeeID>
    <EName>今日</EName>
    <ESex>男</ESex>
    <EAge>25</EAge>
    <EPlace>吉林省长春市</EPlace>
    <EMoney>5000</EMoney>
  </Table>
//此例子为: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("<script>alert('修改成功')</script>");
                    }
                }
            }
        }
//重新保存回xml
        doc.Save(Server.MapPath("Test.xml"));
        Response.Write("<script>location='Default.aspx'</script>");
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值