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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - y9902

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