C# XML操作之一:使用XmlDocument来读写
所有代码都在同一个类中,含有对象
XmlDocument doc = new XmlDocument();
新建XML,并且写入内容
private void button4_Click(object sender, EventArgs e) { doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null)); XmlElement newbook = doc.CreateElement("book"); newbook.SetAttribute("genre", "Mystery"); newbook.SetAttribute("publicationdate", "2001"); newbook.SetAttribute("ISBN", "123345525"); XmlElement newTitle = doc.CreateElement("title"); newTitle.InnerText = "The Case of The missing cookie"; newbook.AppendChild(newTitle); XmlElement newAuthor = doc.CreateElement("Author"); newAuthor.InnerText = "James Lorain"; newbook.AppendChild(newAuthor); if(doc.DocumentElement==null) doc.AppendChild(newbook); XmlTextWriter tr = new XmlTextWriter("newbook.xml",Encoding.UTF8); tr.Formatting = Formatting.Indented; doc.WriteContentTo(tr); tr.Close(); }
创建的xml文件内容为
往已有XML文件中添加内容
原有books.xml内容如下

<?xml version="1.0" encoding="ISO-8859-1"?> <!-- Copyright w3school.com.cn --> <!-- W3School.com.cn bookstore example --> <bookstore> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> </bookstore>
操作代码
doc.Load("books.xml"); XmlElement newbook = doc.CreateElement("book"); newbook.SetAttribute("genre", "Mystery"); newbook.SetAttribute("publicationdate", "2001"); newbook.SetAttribute("ISBN", "123345525"); XmlElement newTitle = doc.CreateElement("title"); newTitle.InnerText = "The Case of The missing cookie"; newbook.AppendChild(newTitle); XmlElement newAuthor = doc.CreateElement("Authooooor"); newAuthor.InnerText = "James Lorain"; newbook.AppendChild(newAuthor); doc.DocumentElement.AppendChild(newbook); XmlTextWriter tr = new XmlTextWriter("newbook.xml", null); tr.Formatting = Formatting.Indented; doc.WriteContentTo(tr); tr.Close();
结果就是在原来bookstore节点下附加了新的子节点
2种方法搜索所有title节点,并且打印其内容
效果
doc.Load("books.xml"); XmlNodeList nodeList = doc.GetElementsByTagName("title"); //下面代码效果完全相同 XmlNodeList nodeList2 = doc.SelectNodes("/bookstore/book/title");
foreach (XmlNode node in nodeList) { content.Items.Add(node.OuterXml); }
https://blog.csdn.net/sony0732/article/details/2301958
https://www.cnblogs.com/zhangyf/archive/2009/06/03/1495459.html
https://www.cnblogs.com/malin/archive/2010/03/04/1678352.html
https://www.cnblogs.com/a1656344531/archive/2012/11/28/2792863.html
标签:
CSharp
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!