当前位置: 首页 > news >正文

wordpress+添加版权36优化大师下载安装

wordpress+添加版权,36优化大师下载安装,开发公司利用员工身份贷款买房子,天津免费做网站文章目录 1 使用XmlDocument创建XML文档2 使用XmlTextWriter写XML文档3 使用LINQ to XML 的XDocument类4 小结 本文介绍了在winform中使用C#开发语言来创建XML文档的三种方式,并介绍了各自的优缺点。 方法1是使用 XmlDocument创建XML文档,方法2是使用 …

文章目录

      • 1 使用XmlDocument创建XML文档
      • 2 使用XmlTextWriter写XML文档
      • 3 使用LINQ to XML 的XDocument类
      • 4 小结

本文介绍了在winform中使用C#开发语言来创建XML文档的三种方式,并介绍了各自的优缺点。
方法1是使用 XmlDocument创建XML文档,方法2是使用 XmlTextWriter编写XML文档,方法3是使用 LINQ to XML的Document类生成XML文档。

1 使用XmlDocument创建XML文档

使用XmlDocument.CreateXmlDeclaration来声明头部信息,用XmlDocument.CreateElement创建元素,
把创建的头部信息,还有元素添加到文档中,最后保存。元素之间有同级关系,也有父子关系,可以通过设定的Parent来决定两者之间的联系。

CreateXmlDeclaration(string version, string encoding, string standalone)

version: 版本必须写 1.0
encoding: 设置保存到文件的编码方式,默认是UTF-8
stangalone: 该值必须是“yes”或“no”.如果是null或String.empty,保存的时候不在XML声明上写这个特性。

          //创建一个空的XMLXmlDocument document = new XmlDocument();//声明头部XmlDeclaration dec = document.CreateXmlDeclaration("1.0", "utf-8", "yes");document.AppendChild(dec);//创建根节点XmlElement root =  document.CreateElement("Citys");document.AppendChild(root);//创建父元素XmlElement childEle1 = document.CreateElement("Province");//属性childEle1.SetAttribute("level", "发达");//中间文本childEle1.InnerText = "广东";//添加到根节点root.AppendChild(childEle1);//创建子元素XmlElement subchildEle1 = document.CreateElement("City");//属性subchildEle1.SetAttribute("level", "省会");//中间文本subchildEle1.InnerText = "广州";//添加到父结点childEle1.AppendChild(subchildEle1);//创建子元素XmlElement subchildEle2 = document.CreateElement("City");subchildEle2.SetAttribute("level", "一线");//中间文本subchildEle2.InnerText = "深圳";//添加到父结点childEle1.AppendChild(subchildEle2);//创建父元素XmlElement childEle2 = document.CreateElement("Province");//属性childEle2.SetAttribute("level", "发展");//中间文本childEle2.InnerText = "广西";//添加到根节点root.AppendChild(childEle2);//创建子元素XmlElement subchildEle3 = document.CreateElement("City");subchildEle3.SetAttribute("level", "三线");//中间文本subchildEle3.InnerText = "桂林";//添加到父结点childEle2.AppendChild(subchildEle3);//保存文档document.Save(@"D:\temp.xml");

运行结果

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Citys><Province level="发达">广东<City level="省会">广州</City><City level="一线">深圳</City></Province><Province level="发展">广西<City level="三线">桂林</City></Province>
</Citys>

扩展添加注释和字符数字段

            //评论注释XmlComment comment = document.CreateComment("这是一份关于城市省份的文件");root.AppendChild(comment);//字符数字段XmlCDataSection CData;CData = document.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!");childEle2.AppendChild(CData);

运行结果

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Citys><!--这是一份关于城市省份的文件--><Province level="发达">广东<City level="省会">广州</City><City level="一线">深圳</City>
</Province><Province level="发展">广西<![CDATA[All Jane Austen novels 25% off starting 3/23!]]><City level="三线">桂林</City></Province>
</Citys>

2 使用XmlTextWriter写XML文档

XmlTextWriter 类中有一些函数是成对出现的,比如WriteStartDocumentWriteEndDocument; 元素WriteStartElementWriteEndElement,用来标注一个元素的开始和结束,有很强的对应性。

 //创建XmlTextWriter对象XmlTextWriter textWriter = new XmlTextWriter(@"D:\writer.xml", Encoding.UTF8);//xml文档开始textWriter.WriteStartDocument();//写根节点textWriter.WriteStartElement("Citys");//注释textWriter.WriteComment("这是使用XmlTextWriter写的");textWriter.WriteStartElement("Province");textWriter.WriteAttributeString(null, "level", null, "发展");textWriter.WriteString("广西");//添加子项textWriter.WriteStartElement("city");textWriter.WriteAttributeString(null, "level", null, "省会");textWriter.WriteString("南宁");textWriter.WriteEndElement();textWriter.WriteStartElement("city");textWriter.WriteAttributeString(null, "level", null, "旅游");textWriter.WriteString("桂林");textWriter.WriteEndElement();//结束Province广西textWriter.WriteEndElement();textWriter.WriteElementString("Province","广东");//结束整个文档textWriter.WriteEndDocument();textWriter.Close();

运行结果

<?xml version="1.0" encoding="utf-8"?>
<Citys>
<!--这是使用XmlTextWriter写的-->
<Province level="发展">广西<city level="省会">南宁</city><city level="旅游">桂林</city>
</Province>
<Province>广东</Province>
</Citys>

3 使用LINQ to XML 的XDocument类

使用到的命名空间

using System.Xml;
using System.Xml.Linq;

使用LinqTo Xml就很方便,XML文档层级结构多的话,写代码时要注意缩进方便阅读,示例代码:

          XDocument document = new XDocument(new XElement("Citys", new XComment("这是使用Ling写的XML"),new XElement("Province", new XText("广西"), new XElement("city", new XText("南宁"),new  XAttribute("level", "省会")),new XElement("city", new XText("桂林"), new XAttribute("level", "旅游"))),new XElement("Province", new XText("广东"),new XElement("city", new XText("广州"), new XAttribute("level", "省会")),new XElement("city", new XText("深圳"), new XAttribute("level", "一线")))) );document.Save(@"D:\Ling.xml");

运行结果:

<?xml version="1.0" encoding="utf-8"?>
<Citys><!--这是使用Ling写的XML--><Province>广西<city level="省会">南宁</city><city level="旅游">桂林</city></Province><Province>广东<city level="省会">广州</city><city level="一线">深圳</city></Province>
</Citys>

4 小结

1 使用XmlDocument创建XML文档,创建的节点指定父节点,就构成层级关系,最后将最上层的节点添加到Document中保存即可。

2 使用XmlTextWriter编写XML文档时,该类有很强元素开始和结束的对应关系(l例如WriteStartElementWriteEndElement),通过这种嵌套关系来建立元素之间的层级。

3 使用LINQ to XML构建XML文档时,很便利快捷,有种所见即所得的既视感,虽然也是通过嵌套建立的层级关系,但又不像XmlTextWriter的严格接口要求。如果层级比较多,需要规范的代码缩进。

http://www.hkea.cn/news/172066/

相关文章:

  • wordpress wp.net网络优化工程师是做什么的
  • 刷会员网站怎么做外贸如何推广
  • 专做女装的网站网站备案是什么意思
  • 没有网站可以做seo排名吗小学生简短小新闻摘抄
  • 做程序网站需要什么代码宁波seo搜索排名优化
  • 网站建设开发语言新冠病毒最新消息
  • 怎么做1688网站网页制作工具有哪些
  • 一个网站的主题和设计风格最好用的免费建站平台
  • 网站开发主页手机优化游戏性能的软件
  • 怎么做属于自己的域名网站网络策划方案
  • destoon做的网站百度商务合作联系
  • 金山区网站制作网络营销策划书1500字
  • 厦门网站建设制作工具熊猫关键词挖掘工具
  • 徐州网站建设 网站推广百度首页快速排名系统
  • 在线转格式网站怎么做拼多多seo 优化软件
  • 成都理工疫情最新消息贵港seo
  • 网站如何防止攻击怎么自己做一个小程序
  • 企业网站建设英文百度收录
  • wordpress查版本sem和seo的区别
  • 网站设计说明书怎么写网站建设平台官网
  • 有建网站的软件阿里云域名注册万网
  • 站长工具排名分析怎么创建公司网站
  • 网站建设标书四川seo哪里有
  • 接网站开发做多少钱建一个外贸独立站大约多少钱
  • wordpress表单录入seo报告
  • python做网站显示表格星巴克seo网络推广
  • 一个com的网站多少钱管理微信软件
  • 蒙阴网站建设软文代写网
  • 用python做一旅游网站南昌seo计费管理
  • 湖北省建设厅win10优化软件哪个好