首页 > 编程语言 > C# 如何在WINForm程序中创建XML文件
2021
02-26

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文件的资料请关注自学编程网其它相关文章!

编程技巧