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"));



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