2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > C#创建读取写入XML文件的实例代码分享

C#创建读取写入XML文件的实例代码分享

时间:2024-05-26 05:09:35

相关推荐

C#创建读取写入XML文件的实例代码分享

后端开发|C#.Net教程

C#,XML文件

后端开发-C#.Net教程

本篇文章主要介绍了C# 创建,读取,写入XML文件的方法,具有很好的参考价值。下面跟着小编一起来看下吧

电商源码 保密,Vscode右键功能,yermux ubuntu,tomcat慢线程,sqlite返回值-5,wordpress 会员中心插件,前端的主流框架好学么,小爬虫火焰切割机,图床 php,黄晓凯seo,帝国网站管理系统如何更改banner图片,网页底部特效源码,app设计模板lzw

Xml文件如下:

游戏盗号源码,ubuntu离线软件库,爬虫 获取数据 保存,php快捷开启关闭php,天津学seolzw

001.jpg 001.aspx 10 gucas01002.jpg 002.aspx 20 gucas0.jpg 003.aspx 30 gucas03004.jpg 004.aspx 40 gucas04005.jpg 005.aspx 50 gucas05google.jpg 30 google

对XML文件的操作

wap在线打包平台源码,ubuntu下载网卡驱动,爬虫框架scrapy书,PHP青岛,商洛seo公司lzw

/// /// public void CreateXmlFile(string filename) { XmlDocument xmldoc = new XmlDocument(); XmlNode node; node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null); xmldoc.AppendChild(node); XmlNode root = xmldoc.CreateElement("Users"); xmldoc.AppendChild(root); CreateNode(xmldoc, root, "UserName", "zhengyd"); CreateNode(xmldoc, root, "Email", "zhengyd@gucas.ac,cn"); CreateNode(xmldoc, root, "Url", "www.gucas.an,cn"); CreateNode(xmldoc, root, "Age", "27"); try { xmldoc.Save(Server.MapPath(filename)); Response.Write("创建XML文件myxml.xml成功!"); } catch (Exception ex) { Response.Write(ex.Message); } } /// /// public void WriteXmlFile(string file) { XmlDocument xmdoc = new XmlDocument(); try { xmdoc.Load(Server.MapPath(file)); XmlNode root = xmdoc.SelectSingleNode("Advertisements"); if (root != null) {XmlNode node = xmdoc.CreateNode(XmlNodeType.Element, "Ad", null);CreateNode(xmdoc, node, "ImageUrl", "google.jpg");CreateNode(xmdoc, node, "NavigateUrl", "");CreateNode(xmdoc, node, "Impressions", "30");CreateNode(xmdoc, node, "Keyword", "google");root.AppendChild(node); } xmdoc.Save(Server.MapPath(file)); Response.Write("写入XML文件XMLFile.xml成功。

"); } catch(Exception ex) { Response.Write(ex.Message); } } /// /// /// /// /// public void CreateNode(XmlDocument xmldoc, XmlNode parentnode, string name, string value) { XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, name, null); node.InnerText = value; parentnode.AppendChild(node); } /// /// private void ReadFile(string file) { XmlDocument xmldoc = new XmlDocument(); try { xmldoc.Load(Server.MapPath(file)); XmlNode node = xmldoc.SelectSingleNode("Advertisements"); if (node != null) {TreeNode root = new TreeNode();root.Text = node.Name;tvXml.Nodes.Add(root);foreach (XmlNode xnode in xmldoc.SelectNodes("Advertisements/Ad")){TreeNode tnode = new TreeNode();tnode.Text = Server.HtmlEncode("");root.ChildNodes.Add(tnode); foreach (XmlNode xcnode in xnode.ChildNodes) { TreeNode tcnode = new TreeNode(); tcnode.Text = Server.HtmlEncode("" + xcnode.InnerText + ""); tnode.ChildNodes.Add(tcnode); }TreeNode ttnode = new TreeNode();ttnode.Text = Server.HtmlEncode("</" + xnode.Name + "");root.ChildNodes.Add(ttnode);}TreeNode tpnode = new TreeNode();tpnode.Text = Server.HtmlEncode("");tvXml.Nodes.Add(tpnode); } } catch (Exception ex) { Response.Write(ex.Message); } }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。