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", "");
newbook.SetAttribute("ISBN", ""); 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", "");
newbook.SetAttribute("ISBN", "");
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
C# XML操作之一:使用XmlDocument来读写的更多相关文章
- .net学习笔记---xml操作及读写
一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...
- C#操作XML的完整例子——XmlDocument篇
这是一个用c#控制台程序下, 用XmlDocument 进行XML操作的的例子,包含了查询.增加.修改.删除.保存的基本操作.较完整的描述了一个XML的整个操作流程.适合刚入门.net XML操作的 ...
- C#操作XML的完整例子——XmlDocument篇(转载,仅做学习之用)
原文地址:http://www.cnblogs.com/serenatao/archive/2012/09/05/2672621.html 这是一个用c#控制台程序下, 用XmlDocument 进 ...
- 对xml操作
已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...
- 简单的XML操作类
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- C#常用操作类库三(XML操作类)
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- XML Helper XML操作类
写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web ...
- .Net XML操作 <第二篇>
一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...
- C#操作Xml:linq to xml操作XML
LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...
- c#XML操作类的方法总结
using System.Xml;using System.Data; namespace DotNet.Utilities{ /// <summary> /// Xml的操作 ...
随机推荐
- 【agc005d】~K Perm Counting
题目大意 求有多少中1~n的排列,使得\(abs(第i个位置的值-i)!=k\) 解题思路 考虑容斥,\(ans=\sum_{i=0}^{n}(-1)^ig[i](n-i)!(g[i]表示至少有i个位 ...
- Linux系统安装xinetd服务
只需安装xinetd包 安装包 #yum -y install xinetd 安装成功后即可 service xinetd start service xinetd stop service xine ...
- Flash上传超大文件解决方案
文件夹数据库处理逻辑 public class DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject() ...
- B/S文件夹上传下载组件
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...
- jQuery事件之解绑事件
语法: $(selector).unbind([eventType][,handler(eventObject)]); 返回值:jQuery 参数解释: eventTypey:类型:String以后包 ...
- B. Interesting Array(线段树)
B. Interesting Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- AcWing:111. 畜栏预定(贪心 + 小根堆)
有N头牛在畜栏中吃草. 每个畜栏在同一时间段只能提供给一头牛吃草,所以可能会需要多个畜栏. 给定N头牛和每头牛开始吃草的时间A以及结束吃草的时间B,每头牛在[A,B]这一时间段内都会一直吃草. 当两头 ...
- 关于自定义sparkSQL数据源(Hbase)操作中遇到的坑
自定义sparkSQL数据源的过程中,需要对sparkSQL表的schema和Hbase表的schema进行整合: 对于spark来说,要想自定义数据源,你可以实现这3个接口: BaseRelatio ...
- android 实例-弱引用示例 Handler正确使用方法
实际问题 android 习惯性问题:在使用handler的时候喜欢使用内部类形式. private final Handler handler = new Handler(){ @Override ...
- springboot非web项目
使用CommandRunner @SpringBootApplication public class CrmApplication implements ApplicationRunner { @A ...