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

推荐订阅源

S
Security @ Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
Intezer
S
Securelist
Google DeepMind News
Google DeepMind News
S
Schneier on Security
T
Troy Hunt's Blog
Help Net Security
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
Security Archives - TechRepublic
Security Archives - TechRepublic
O
OpenAI News
博客园 - Franky
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
TaoSecurity Blog
TaoSecurity Blog
MyScale Blog
MyScale Blog
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
博客园_首页
S
Security Affairs
PCI Perspectives
PCI Perspectives
WordPress大学
WordPress大学
C
Cisco Blogs
Recent Announcements
Recent Announcements
L
LangChain Blog
GbyAI
GbyAI
F
Fortinet All Blogs
N
News and Events Feed by Topic
T
Tor Project blog
IT之家
IT之家
P
Palo Alto Networks Blog
D
DataBreaches.Net
小众软件
小众软件
宝玉的分享
宝玉的分享
F
Full Disclosure

博客园 - 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;
        }