asp.net 读写XML文件

读取XML内容

            XmlDocument xml=new XmlDocument();
            xml.Load(Server.MapPath("text.xml"));
            XmlNode node = xml.SelectSingleNode("root");//根节点
            XmlNode nd = node.SelectSingleNode("city");//根据根节点获取子节点
            TextBox1.Text = nd.InnerText;//读出节点“city”的内容
            XmlNodeList nodelist = xml.SelectNodes("root");
            //遍历所有节点的内容
            foreach (XmlNode node1 in nodelist)
            {
                ListBox1.Items.Add(node1.InnerText);
            }


更新XML文件

    XmlDocument xml = new XmlDocument();
            xml.Load(Server.MapPath("text.xml"));
            XmlNode node = xml.SelectSingleNode("root");//根节点
            XmlNode nd = node.SelectSingleNode("city");//根据根节点获取子节点
            TextBox1.Text = nd.InnerText;//读出节点“city”的内容
            nd.InnerText = "胡楼";
            xml.Save(Server.MapPath("text.xml"));


创建XML文件

        XmlDocument xml = new XmlDocument();
            XmlNode node = xml.CreateXmlDeclaration("1.0","big5","");
            xml.AppendChild(node);
            //新建根节点
            XmlNode root = xml.CreateElement("rui");
            xml.AppendChild(root);
            XmlNode a=xml.CreateNode(XmlNodeType.Element,"city","");
            a.InnerText = "深圳";
            root.AppendChild(a);
            XmlNode b=xml.CreateNode(XmlNodeType.Element,"address","");
            b.InnerText = "MSI";
            root.AppendChild(b);
            XmlNode c = xml.CreateNode(XmlNodeType.Element,"time","");
            c.InnerText = "8:30";
            root.AppendChild(c);

            //新建一个父节点
            XmlNode item = xml.CreateNode(XmlNodeType.Element,"item","");
            root.AppendChild(item);//追加到根节点
            XmlNode a1 = xml.CreateNode(XmlNodeType.Element,"city","");
            a1.InnerText = "郑州";
            item.AppendChild(a1);
            XmlNode b1 = xml.CreateNode(XmlNodeType.Element,"adderss","");
            b1.InnerText = "郑州大学";
            item.AppendChild(b1);
            XmlNode c1 = xml.CreateNode(XmlNodeType.Element,"time","");
            c1.InnerText = "8:30";
            item.AppendChild(c1);
            xml.Save(Server.MapPath("zrh.xml"));



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章