C# 如何在WINForm程序中创建XML文件

<?xml version="1.0" encoding="gb2312"?>
<FilesInformation>
  <version>1.0.1818.42821</version>
  <description>说明</description>
  <FileItem 
  FileName="name"
  FileVersion="sdf"
  FileLength="sdf"
  FileCreationTime="sd"
  />
</FilesInformation>
string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;   

获取和设置包含该应用程序的目录的名称

File.Exists(path + XmlFileName) 

File.Exists是判断文件是否存在,传入参数为路径+文件名

XmlDocument xmlDoc = new XmlDocument();    

这一句是创建一个XmlDocument对象

XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);   

这一句是添加xml文件头的声明

xmlDoc.AppendChild(xmlSM); 

这一句是将创建的XmlDocument对象追加到xml文件声明后面

XmlElement DeviceTree = xmlDoc.CreateElement("DeviceTree"); 

这一句为创建一个标签名为DeviceTree的节点

DeviceTree.SetAttribute("name", "设备树");

这一句设置节点的name属性为设备树

xmlDoc.AppendChild(DeviceTree);

这一句是将创建的节点添加到开始创建的XmlDocument对象中

xmlDoc.Save(path + XmlFileName);

最后是保存创建好的xml文件

方法1:

private void button1_Click(object sender, EventArgs e) 
{     
XmlDocument xmlDoc = new XmlDocument();           //建立Xml的定义声明        
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);        
xmlDoc.AppendChild(dec);           //创建根节点        
XmlElement root = xmlDoc.CreateElement("FilesInformation");        
xmlDoc.AppendChild(root);       
XmlElement version = xmlDoc.CreateElement("version");      version.InnerText = "1.0.1818.42821";     
root.AppendChild(version);         
XmlElement description = xmlDoc.CreateElement("description");     
description.InnerText = "说明";     
root.AppendChild(description);       
XmlElement fileItem = xmlDoc.CreateElement("FileItem");     
fileItem.SetAttribute("FileName", "name");     
fileItem.SetAttribute("FileVersion", "xx");     
fileItem.SetAttribute("FileLength", "xxx");     
fileItem.SetAttribute("FileCreationTime", "xxxx");     
root.AppendChild(fileItem);          
xmlDoc.Save("test.xml");   
 }

方法2:

XmlDocument xmldoc = new XmlDocument();
               XmlText xmltext;
 
               //声明
               XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
               xmlnode.InnerText += " encoding=\"GB2312\"";
               xmldoc.AppendChild(xmlnode);
 
               //添加根节点
               XmlElement xmlelementroot = xmldoc.CreateElement("", "Config", "");
               //根节点包含节点文本时会造成XML文档结构的混乱
               //xmltext = xmldoc.CreateTextNode("配置信息");
               //xmlelementroot.AppendChild(xmltext);
               xmldoc.AppendChild(xmlelementroot);
 
               //添加一个元素
               XmlElement xmlelement1 = xmldoc.CreateElement("", "DTL", "");
               xmltext = xmldoc.CreateTextNode("2010-10-25");
               xmlelement1.AppendChild(xmltext);
               xmldoc.ChildNodes.Item(1).AppendChild(xmlelement1);
 
               //添加另一个元素
               XmlElement xmlelement2 = xmldoc.CreateElement("", "DTF", "");
               xmltext = xmldoc.CreateTextNode("2011-02-10");
               xmlelement2.AppendChild(xmltext);
               xmldoc.ChildNodes.Item(1).AppendChild(xmlelement2);
 
               //保存
               xmldoc.Save(Environment.CurrentDirectory+\\111.xml);

方法3:

XmlTextWriter xmlwriter = new XmlTextWriter(getPath(), Encoding.Default);
                xmlwriter.Formatting = Formatting.Indented;
                xmlwriter.Indentation = 4;
 
                xmlwriter.WriteStartDocument();
                xmlwriter.WriteStartElement("", "Config", "");
 
                xmlwriter.WriteStartElement("", "DTL", "");
                xmlwriter.WriteString("2010-10-25");
                xmlwriter.WriteEndElement();
 
                xmlwriter.WriteStartElement("", "DTF", "");
                xmlwriter.WriteString("2011-02-10");
                xmlwriter.WriteEndElement();
 
                xmlwriter.WriteEndElement();
                xmlwriter.WriteEndDocument();
 
                xmlwriter.Flush();
                xmlwriter.Close();

上面代码中的getPath()是自定义的一个获取文件路径加名称的方法,请根据自己实际情况修改!我一般设定为

Environment.CurrentDirectory+\\111.xml

总的来说还是方法三比较容易理解,简单易用,也是我常用的方法!

希望对各位有所帮助!

以上就是C# 如何在WINForm程序中创建XML文件的详细内容,更多关于c# 创建XML文件的资料请关注我们其它相关文章!

(0)

相关推荐

  • C# XML中的转义字符操作

    C# XML中 <. > 等转义字符转为 <.>等符号 using System.IO; using System.Xml; public string ToXML(string str) { StringReader Reader = new StringReader(str); XmlDocument xml = new XmlDocument(); xml.Load(Reader); return xml.InnerText.ToString(); } C# XML中<

  • C#操作XML文件步骤

    编写C#程序的时候,我们都遇到过配置文件,而如今绝大多数的配置文件都是用XML写的.所以在处理的时候就需要操作XML文件.那么C#如何操作XML文件那?下面跟我一起操作 1.先用VS工具创建个控制台程序,导入操作XML的命名空间System.Xml,如下图所示 2.然后我们运用命名空间里的XmlDocument对象实例化,如下图所示 3.接下来就是创建XML的第一行描述信息了,如下图所示,设置编码格式 4.添加完第一行描述信息后就需要给XML添加节点了,如下图所示,调用CreateElement

  • C#使用XmlDocument或XDocument创建xml文件

    使用XmlDocument或XDocument创建xml文件,具体内容如下 需引用:System.Xml; System.Xml.Linq; 1.使用XmlDocument创建xml(入门案例) static void Main(string[] args) { //使用XmlDocument创建xml XmlDocument xmldoc = new XmlDocument(); XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0&

  • c# 读取XML文件的示例

    如下XML文件:(算是一个属性值比较多的xml文件...读取该Xml算是我在公司实际的一个任务) <?xml version="1.0" encoding="UTF-8"?> <serverset> <devset PrintNumber="1" ControlBarcode="" ControlEBarcode="" ControlPIX="" Print

  • c# RSA非对称加解密及XML&PEM格式互换方案

    最近因考虑接口安全问题,有实现给WEB API实现统一的参数鉴权功能,以防止请求参数被篡改或重复执行,参数鉴权方法基本与常见的鉴权思路相同,采用(timestamp+sign),而我为了防止timestamp被更改,sign算法(timestamp+相关参数排序.格式化后拼接再MD5)也因为在前端是不安全的,故对timestamp采取使用非对称加解密,以尽可能的保证生成的sign不易被破解或替换: RSA加解密(即:非对称加解密) 生成公钥.私钥对方法(C#),生成出来后默认都是XML格式: p

  • C# xmlSerializer简单用法示例

    本文实例讲述了C# xmlSerializer简单用法.分享给大家供大家参考,具体如下: 先上实体类 public class Entity { public Entity() { } public Entity(string c, string f) { name = c; school = f; } public string name; public string school; } 使用时声明 List<Entity> entityList=null; XmlSerializer xs

  • c# winform读取xml文件创建菜单的代码

    复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using WinformMenu.Helper;using System.Xml; namespace WinformMen

  • C# 读写XML文件实例代码

    C#史上最简单读写xml文件方式,创建控制台应用程序赋值代码,就可以运行,需要改动,请自行调整 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace ConsoleApp1 { class Program { public cons

  • C# WinForm开发中使用XML配置文件实例

    本文介绍在使用C#开发WinForm程序时,如何使用自定义的XML配置文件.虽然也可以使用app.config,但命名方面很别扭. 我们在使用C#开发软件程序时,经常需要使用配置文件.虽然说Visual Studio里面也自带了app.config这个种配置文件,但用过的朋友都知道,在编译之后,这个app.config的名称会变成app.程序文件名.config,这多别扭啊!我们还是来自己定义一个配置文件吧. 配置文件就是用来保存一些数据的,那用xml再合适不过.那本文就介绍如何使用XML来作为

  • C# XML字符串包含特殊字符的处理转换方法小结

    为了能正常输出XML格式的内容,必须要对不被XML允许的那些特殊字符进行转换.本文介绍的正是如何使用C#判断XML字符串是否含特殊字符并进行转换. 以下是几个特殊字符的对应实体. < < 小于号 > > 大于号 & & 和 &apos; ' 单引号 " " 双引号 在C#中,直接调用C#提供的方法,保存之后就会自动将特殊字符转为对应实体: string s =System.Security.SecurityElement.Escape(s

随机推荐