使用XmlDocument操作xml,常用操作包括:创建xml文件,编辑xml文件,读取xml文件。主要包括对节点的创建、编辑,属性的的创建、编辑,以及使用XPath进行查找。看下图:
代码如下:
??????? //创建xml protected void Button1_Click(object sender, EventArgs e) ??????? { ??????????? XmlDocument objXmlDocument = new XmlDocument(); ??????????? //xml定义声明 ??????????? XmlDeclaration objXmlDeclaration = objXmlDocument.CreateXmlDeclaration("1.0", "utf-8", null); ??????????? objXmlDocument.AppendChild(objXmlDeclaration); ??????????? //根节点:软件 ??????????? XmlElement root = objXmlDocument.CreateElement("Softs"); ??????????? objXmlDocument.AppendChild(root); ??????????? //具体软件节点 ??????????? XmlElement soft = objXmlDocument.CreateElement("Soft"); ??????????? root.AppendChild(soft); ??????????? //软件名称 ??????????? XmlElement name = objXmlDocument.CreateElement("Name"); ??????????? name.InnerText = "VeryCodes DHCMS"; ??????????? soft.AppendChild(name); ??????????? //软件作者 ??????????? XmlElement author = objXmlDocument.CreateElement("Author"); ??????????? author.InnerText = "BOSSMA"; ??????????? soft.AppendChild(author); ??????????? //软件介绍 ??????????? XmlElement desc = objXmlDocument.CreateElement("Description"); ??????????? desc.InnerText = "VeryCodes DHCMS 是一个优秀的CMS系统。"; ??????????? soft.AppendChild(desc); ??????????? //添加一个属性:添加日期 ??????????? soft.SetAttribute("AddDate", DateTime.Now.ToShortDateString()); ??????????? //保存文件到硬盘 ??????????? objXmlDocument.Save(Server.MapPath("Softs.xml")); ??????????? ClientScript.RegisterClientScriptBlock(this.GetType(), "CreateXml", "alert('xml创建成功');", true); ??????? } //读取xml ??????? protected void Button2_Click(object sender, EventArgs e) ??????? { ??????????? XmlDocument objXmlDocument = new XmlDocument(); ??????????? //加载XML文件 ??????????? objXmlDocument.Load(Server.MapPath("Softs.xml")); ??????????? //选择根节点 ??????????? XmlNode root = objXmlDocument.SelectSingleNode("Softs"); ??????????? //所有的软件节点 ??????????? XmlNodeList softList = root.ChildNodes; ??????????? //读取数据 ??????????? StringBuilder softString = new StringBuilder(); ??????????? softString.Append("<br/>"); ??????????? foreach (XmlNode node in softList) ??????????? { ??????????????? softString.Append("软件名称:"); ??????????????? softString.Append(node.SelectSingleNode("Name").InnerText); ??????????????? softString.Append("<br/>"); ??????????????? softString.Append("软件作者:"); ??????????????? softString.Append(node.SelectSingleNode("Author").InnerText); ??????????????? softString.Append("<br/>"); ??????????????? softString.Append("软件简介:"); ??????????????? softString.Append(node.SelectSingleNode("Description").InnerText); ??????????????? softString.Append("<br/>"); ??????????????? softString.Append("添加日期:"); ??????????????? softString.Append(node.Attributes["AddDate"].Value); ??????????????? softString.Append("<br/>"); ??????????? } ??????????? Literal2.Text = softString.ToString(); ??????? } //编辑xml ??????? protected void Button3_Click(object sender, EventArgs e) ??????? { ??????????? XmlDocument objXmlDocument = new XmlDocument(); ??????????? //加载XML文件 ??????????? objXmlDocument.Load(Server.MapPath("Softs.xml")); ??????????? //获取节点 ??????????? XmlElement node = (XmlElement)objXmlDocument.SelectSingleNode("Softs/Soft[Name='VeryCodes DHCMS']"); ??????????? //更新属性值 ??????????? node.SetAttribute("AddDate", DateTime.Now.ToString()); ??????????? //获取Name节点,并更新值 ??????????? XmlNode nameNode = node.SelectSingleNode("Name"); ??????????? nameNode.InnerText = "VeryCodes DHCMS V1.0"; ??????????? //保存文件到硬盘 ??????????? objXmlDocument.Save(Server.MapPath("Softs.xml")); ??????????? ClientScript.RegisterClientScriptBlock(this.GetType(), "CreateXml", "alert('xml更新成功');", true); ??????? }
关键字: XML xmldocument xpath
发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论