xml中的常用操作示例
?
?
XmlDocument xmlDoc;
??????????? xmlDoc = new XmlDocument();
??????????? xmlDoc.Load(Application.StartupPath+"\\pointConfig.xml");
??????????? XmlNode xmldocSelect = xmlDoc.SelectSingleNode("root");//xml文件的根結點是user
??????????? xmldocSelect = xmldocSelect.SelectNodes("pointList")[0];
??????????? XmlElement el = xmlDoc.CreateElement("point");//添加person節點
??????????? el.SetAttribute("index", "1");//添加屬性名,及屬性值
??????????? el.SetAttribute("trackNo", "1");
??????????? XmlElement xesub1 = xmlDoc.CreateElement("arriveTime");
??????????? xesub1.InnerText = "2012-05-05 13:05:00";
??????????? el.AppendChild(xesub1);
??????????? XmlElement xesub2 = xmlDoc.CreateElement("lat");
??????????? xesub2.InnerText = "133";
??????????? el.AppendChild(xesub2);
??????????? XmlElement xesub3 = xmlDoc.CreateElement("lon");
??????????? xesub3.InnerText = "39";
??????????? el.AppendChild(xesub3);
??????????? XmlElement xesub4 = xmlDoc.CreateElement("ico");
??????????? xesub4.SetAttribute("defaultDisply", "true");
??????????? xesub4.InnerText = @"http://localhost/a.gif";
??????????? el.AppendChild(xesub4);
??????????? XmlElement xesub5 = xmlDoc.CreateElement("text");
??????????? xesub5.InnerText = "cccc";
??????????? xesub5.SetAttribute("defaultDisply", "true");
??????????? el.AppendChild(xesub5);
??????????? XmlElement xesub6 = xmlDoc.CreateElement("tag");
??????????? xesub6.SetAttribute("action", "click");
??????????? el.AppendChild(xesub4);
??????????? XmlElement e2 = (XmlElement)el.Clone();
??????????? e2.SelectNodes("lon")[0].InnerText="134";
??????????? e2.SetAttribute("index", "2");
??????????? xmldocSelect.AppendChild(el);//將節點person加到根結點xmldocSelect下
??????????? xmldocSelect.AppendChild(e2);
??????????? xmlDoc.Save("trytry.xml");//保存,這個十分重要,否則沒有數據
?
?
private XmlDataDocument xmlDoc;//聲明全局變量xmlDoc,類型是XmlDataDocument
???? protected void Page_Load(object sender, EventArgs e)
???? {
?
??? }
?
//聲明加載xml文件的方法
???? private void xmlLoad()
???? {
???????? xmlDoc = new XmlDataDocument();
???????? xmlDoc.Load(Server.MapPath("trytry.xml"));//文件名是trytry.xml
???? }
?
//點擊添加按鈕,添加數據
???? protected void ButtonAdd_Click(object sender, EventArgs e)
???? {
???????? xmlLoad();//調用加載xml文件的方法
???????? XmlNode xmldocSelect = xmlDoc.SelectSingleNode("user");//xml文件的根結點是user
?
??????? XmlElement el = xmlDoc.CreateElement ("person");//添加person節點
???????? el.SetAttribute("name", "云云");//添加屬性名,及屬性值
???????? el.SetAttribute("sex", "女");
???????? el.SetAttribute("age", "25");
?
??????? XmlElement xesub1 = xmlDoc.CreateElement("pass");//添加person節點的子節點
???????? xesub1.InnerText = "123";//節點pass下的值是123
???????? el.AppendChild(xesub1);//將節點pass添加到父節點person下
???????? XmlElement xesub2 = xmlDoc.CreateElement("address");
???????? xesub2.InnerText = "中國西安";
???????? el.AppendChild(xesub2);
?
??????? xmldocSelect.AppendChild(el);//將節點person加到根結點xmldocSelect下
???????? xmlDoc.Save(Server.MapPath("trytry.xml"));//保存,這個十分重要,否則沒有數據
?
??? }
?
//點擊修改按鈕,修改數據
???? protected void ButtonUpdata_Click(object sender, EventArgs e)
???? {
???????? xmlLoad();//調用加載xml文件的方法
???????? XmlNodeList list = xmlDoc.SelectSingleNode("user").ChildNodes;//獲得根節點user下的所有子節點
?
?
?
//遍歷所有子節點
?
??????? foreach (XmlNode node in list)
???????? {
???????????? XmlElement xe = (XmlElement)node;//把節點node轉換成XmlElement型
???????????? if (xe.GetAttribute("name") == "云云")//如果節點的屬性name的值是"云云"
???????????? {
???????????????? xe.SetAttribute("name","哈哈");//把節點的name屬性改為"哈哈"
???????????? }
???????????? XmlNodeList sublist = xe.ChildNodes;//再獲得該節點的子節點
???????????? foreach (XmlNode subnode in sublist)
???????????? {
???????????????? XmlElement subxe = (XmlElement)subnode ;
???????????????? if (subxe.Name == "address")//如果節點是"address"
???????????????? {
???????????????????? if (subxe.InnerText == "中國西安")//如果該節點下的值是"中國西安"
???????????????????????? subxe.InnerText = "中國三河";
???????????????????? break;
???????????????? }
???????????? }
???????? }
???????? xmlDoc.Save(Server.MapPath("trytry.xml"));
???? }
?
//點擊刪除按鈕,刪除數據
???? protected void ButtonDele_Click(object sender, EventArgs e)
???? {
???????? xmlLoad();
???????? XmlNodeList list = xmlDoc.SelectSingleNode("user").ChildNodes;
???????? foreach (XmlNode node in list)
???????? {
???????????? XmlElement xe = (XmlElement)node;
???????????? if (xe.GetAttribute("name") == "哈哈")
???????????? {
???????????????? xe.RemoveAttribute("name");//刪除屬性
???????????? }
???????????? else
???????????????? xe.RemoveAll();//刪除節點
???????? }
?
??????? xmlDoc.Save(Server.MapPath("trytry.xml"));
??????
???? }
總結
以上是生活随笔為你收集整理的xml中的常用操作示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 战斗系统的伪原创工具
- 下一篇: C#中深拷贝对象的简单方法