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

1
2
3
4
5
6
7
8
9
10
11
<?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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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#知识,请扫描下方公众号二维码

 

 

需加微信交流群的,请加小编微信号z438679770,切记备注 加群,小编将会第一时间邀请你进群!

posted @   zls366  阅读(306)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示