惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
V
Visual Studio Blog
H
Help Net Security
Engineering at Meta
Engineering at Meta
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
博客园 - 【当耐特】
B
Blog
Stack Overflow Blog
Stack Overflow Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园 - 司徒正美
博客园 - 叶小钗
Y
Y Combinator Blog
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - y9902

修改系统分区后挂载的问题 - y9902 - 博客园 雅虎存在严重安全漏洞,可以轻松获得账户的控制权 I AM BACK 深入浅出 ck1.in/N.JS 新加了一个友情链接 雅虎书签存在严重安全漏洞,可以轻松获得账户的控制权 Deamhost 的当机页面 一个美丽,智慧,心善的明星:张瑶 Windows的蓝屏秀 c# 时间戳 推荐一个网站 vista 自带的IE7 的 User Agent 国外虚拟主机购买 卡巴斯基互联网安全套装V6.0个人版激活码 域名的巨大潜力 一个操作firebird的helper类 如何修复DNS? GzipStream 相关(操作steam绝对经典) pcc的文件格式面世了
Building XML File in C#
y9902 · 2006-08-18 · via 博客园 - y9902

Why?
Though I found a lot of information on how to read a XML file, I wasn't able to find a complete solution for building XML structure and writing it to file system for my project. I hope this tutorial (or code sample) is helpful to some people.

For Whom?
This tutorial is designed for people with basic XML knowledge, to serve as a reference when building a XML file, the idea is very simple. Finding all the classes, methods needed is actually what takes time, thus the tutorial concentrates on C# language.

Different from writing a regular file.
When we open a file to write, there are many options available: override if already exists, append, etc. It surprises me that there is no single class in C# that does the following: "Read following XML file, if the XML file doesn't exist, create new XML file (Correct me if I am wrong)." Either XmlTextWriter or XmlDocument satisfy the requirement, the combination of the two works well.

Code Sample

Sample Output

using System.Xml;

        void WriteXML()
        {
            try
            {
                                
                string filename = "XML"+DateTime.Now.Day + ".xml";

                XmlDocument xmlDoc = new XmlDocument();

                try
                {
                    xmlDoc.Load(filename);
                }
                catch(System.IO.FileNotFoundException)
                {
                    
                    XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                    xmlWriter.WriteStartElement("Root");
                    
                    
                    
                    
                    xmlWriter.Close();
                    xmlDoc.Load(filename);
                }
                XmlNode root = xmlDoc.DocumentElement;
                XmlElement childNode = xmlDoc.CreateElement("childNode");
                XmlElement childNode2 = xmlDoc.CreateElement("SecondChildNode");
                XmlText textNode = xmlDoc.CreateTextNode("hello");
                textNode.Value = "hello, world";
                
                root.AppendChild(childNode);
                childNode.AppendChild(childNode2);
                childNode2.SetAttribute("Name", "Value");
                childNode2.AppendChild(textNode);
                
                textNode.Value = "replacing hello world";
                xmlDoc.Save(filename);
            }
            catch(Exception ex)
            {
                WriteError(ex.ToString());
            }
        }

        void WriteError(string str)
        {
            outputBox.Text = str;
        }