<?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; node
获取和设置包含该应用程序的目录的名称微信
File.Exists(path + XmlFileName) orm
File.Exists是判断文件是否存在,传入参数为路径+文件名xml
XmlDocument xmlDoc = new XmlDocument(); 对象
这一句是建立一个XmlDocument对象blog
XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); ip
这一句是添加xml文件头的声明utf-8
xmlDoc.AppendChild(xmlSM); element
这一句是将建立的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#知识,请扫描下方公众号二维码
需加微信交流群的,请加小编微信号z438679770,切记备注 加群,小编将会第一时间邀请你进群!