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的操作 ...
随机推荐
- webuploader如何判断是否上传的是空文件?
在'beforeFileQueued'事件中可以判断: // 当有文件被添加进队列的时候 uploader.on( 'beforeFileQueued', function( file ) { if( ...
- [CTF]Heap vuln -- unlink
0x00: 起因 一直在堆的漏洞利用中不得要领,之前ZCTF又是三个堆的利用,血崩,chxx表哥给写了一个heap的pwn,学习学习. 0x01: 关于heap的unlink的漏洞利用,出的很早,在低 ...
- hdu 5821 Ball 思维题
Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- K 短路
这种东西到现在才学-- 考虑 \(T\) 为根的最短路树,一条路径一定是树上边和非树边交错. 我们只管非树边,对于一条路径,非树边构成一个序列 \(L\),相邻两条路径 \(\left(u_1,v_1 ...
- Spring——代理实现AOP
一.静态代理实现 1.接口(抽象主题) 2.接口的实现类(真实主题) 3.代理类(代理主题) 4.测试类: ApplicationContext context=new ClassPathXmlApp ...
- Marcin and Training Camp
D. Marcin and Training Camp 参考:D. Marcin and Training Camp 思路:首先先确定最大成员的\(a_i\),因为不能够某个成员i认为自己比其他所有成 ...
- Marked
哈夫曼树 2-sat问题 线性代数基础 矩阵和行列式基础 可并堆1 可并堆2 概率与期望概念 Kruskcl重构树1 Kruskcl重构树2 匈牙利算法 带权并查集 C++参考手册 尺取法 AC自动机 ...
- <context:component-scan>标签报错解决方案
- 如何在Linux上安装Composer
我们可以通过 Composer 镜像安装 composer ,在这之前,请确认您的机器上已经安装了PHP,并可以通过php来执行命令.如果您的服务器上必须通过全路径来执行(/usr/local/php ...
- hadoop学习笔记以及遇到的坑整理(长期更新)
1.要看官方文档 http://hadoop.apache.org/docs/current/ 2.start-dfs.sh时提示rcmd: socket: Permission denied 解决方 ...