波斯马BOSSMA Information Technology

XmlDocument操作xml常用操作

发布时间:2010年9月27日 / 分类:ASP.NET, DOTNET / 9,043 次浏览 / 评论

使用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);
??????? }
本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自波斯马,原文地址《XmlDocument操作xml常用操作

关键字:

建议订阅本站,及时阅读最新文章!
【上一篇】 【下一篇】

发表评论