欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-14 23:10:09
1.0.1818...
<?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文件的资料请关注其它相关文章!

相关标签: c# winform XML