打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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")

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

希望对各位有所帮助!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在C#.net中怎么操作XML
C# 写入XML文档三种方法详细介绍
C#操作xml
C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例(转载)...
.net中XML的简单应用 - - CSDN技术中心
Unity3D研究院之使用 C#合成解析XML与JSON(四十一) | 雨松MOMO程序研究院
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服