ASP.NET 操作XML文件,超簡單的方法

原创 2015年08月14日 14:17:58

XML文件:

<?xml version="1.0" encoding="big5"?>
<root>
  <person>
    <id>4</id>
    <name>小華</name>
    <sex>男</sex>
    <age>20</age>
  </person>
  <person>
    <id>1</id>
    <name>小張</name>
    <sex>男</sex>
    <age>21</age>
  </person>
  <person>
    <id>2</id>
    <name>小瑞</name>
    <sex>女</sex>
    <age>22</age>
  </person>
  <person>
    <id>3</id>
    <name>小錦</name>
    <sex>女</sex>
    <age>17</age>
  </person>
</root>

ASP.NET代碼:讀取XML

protected void Btn_onclick(object sender, EventArgs e)
        {
            Label1.Text = "Name:";
            Label2.Text = "Sex:";
            Label3.Text = "Age:";
            XmlDocument xml = new XmlDocument();
            xml.Load(Server.MapPath("test.xml"));
            if (xml != null)
            {
                XmlNode root = xml.SelectSingleNode("root");
                XmlNodeList rootlist = root.ChildNodes;
                foreach (XmlNode xn in rootlist)
                    {

                        if (xn.SelectSingleNode("id").InnerText==TextBox1.Text.Trim())
                        {
                            Label1.Text += xn.SelectSingleNode("name").InnerText;
                            Label2.Text += xn.SelectSingleNode("sex").InnerText;
                            Label3.Text+=xn.SelectSingleNode("age").InnerText;
                        }

                    }
            }
        }

ASP.NET代碼:添加XML節點

 protected void Button1_Click(object sender, EventArgs e)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(Server.MapPath("test.xml"));
            if (xml != null)
            {
                XmlNode root = xml.SelectSingleNode("root");
                XmlElement person = xml.CreateElement("person");
                XmlElement id = xml.CreateElement("id");
                id.InnerText = "5";
                person.AppendChild(id);
                XmlElement name = xml.CreateElement("name");
                name.InnerText = "小文";
                person.AppendChild(name);
                XmlElement sex = xml.CreateElement("sex");
                sex.InnerText = "男";
                person.AppendChild(sex);
                XmlElement age = xml.CreateElement("age");
                age.InnerText = "18";
                person.AppendChild(age);
                root.AppendChild(person);
                xml.Save(Server.MapPath("test.xml"));

              

            }
        }

ASP.NET代碼:刪除XML節點

 protected void Button2_Click(object sender, EventArgs e)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(Server.MapPath("test.xml"));
            if (xml != null)
            {
                XmlNode root = xml.SelectSingleNode("root");
                XmlNodeList personlist = root.ChildNodes;
                foreach (XmlNode xn in personlist)//遍歷root的子節點,只有person是root的節點
                {
                    if (xn.SelectSingleNode("id").InnerText== "4" && xn.SelectSingleNode("name").InnerText == "小文")
                    {
                        root.RemoveChild(xn);

                    }
                }
                xml.Save(Server.MapPath("test.xml"));


            }

        }


收藏助手
不良信息举报
您举报文章:ASP.NET 操作XML文件,超簡單的方法
举报原因:
原因补充:

(最多只允许输入30个字)