/*******************************************************************/ //写入Xml 元素
XmlDocument xmlDoc = new XmlDocument(); //创建对象 XmlNode xmlNode = xmlDoc.SelectSingleNode("bookstore"); //选择节点 XmlElement xmlElem1 = xmlDoc.CreateElement("book"); //创建节点 XmlElement xmlElem2 = xmlDoc.CreateElement("title"); //创建节点 xmlElem1.AppendChild(xmlElem2); //添加到节点中 xmlElem2 = xmlDoc.CreateElement("author"); xmlElem1.AppendChild(xmlElem2); xmlElem2 = xmlDoc.CreateElement("price"); xmlElem1.AppendChild(xmlElem2); xmlNode.AppendChild(xmlElem1); xmlDoc.Save(xmlFilePath); //保存 XML 文件 /******************************************************************/ /******************************************************************/ //修改 Xml 元素 XmlDocument xmlDoc = new XmlDocument(); //创建对象 XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes; //获取节点下的子节点列表 foreach (XmlNode xn in xnl) //遍历所有节点 if (xe.GetAttribute("address") == "DelAll") //如果元素属性找到 XmlNodeList xnl2 = xe.ChildNodes; //获取它的子节点 foreach (XmlNode xn2 in xnl2) //遍历所有学点 xmlDoc.Save(xmlFilePath); break; //退出 break; //退出 /******************************************************************/ //删除节点 XmlDocument xmlDoc = new XmlDocument(); XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes; int i = xnl.Count; while (--i > -1) if (xe.GetAttribute("address") == "Del") } xmlDoc.Save(xmlFilePath); /******************************************************************/
XmlDocument xmlDoc = new XmlDocument(); XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes; this.Response.Write("Address Title"); foreach (XmlNode xn in xnl) this.Response.Write("<br/>" + xe.GetAttribute("address")); XmlNodeList xnl2 = xe.ChildNodes; foreach (XmlNode xn2 in xnl2) this.Response.Write(" " + xn2.InnerText); //元素与节点的区别 /******************************************************************/ xmlDoc.AppendChild(xmlNode); //添加节点
for (int i = 0; i < 3; i++) //循环添加节点 XmlElement xmlElem1 = xmlDoc.CreateElement("Test11"); //创建元素 xmlElem.AppendChild(xmlElem1); //添加元素 xmlNode.AppendChild(xmlElem); //添加元素 xmlDoc.Save(xmlFilePath); //保存 /******************************************************************/ while (xtr.Read()) xtr.Close(); |
.net Xml的各种操作
最新推荐文章于 2020-11-24 03:49:26 发布