温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-02-26 13:03:35 来源:亿速云 阅读:214 作者:小新 栏目:开发技术

这篇文章主要介绍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文件”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI