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的操作 ...
随机推荐
- Ion-select and ion-option list styling 自定义样式
https://forum.ionicframework.com/t/ion-select-and-ion-option-list-styling/117028
- http 中文乱码
string RawUrl = request.Request.RawUrl; string cc= HttpUtility.ParseQueryString(RawUrl.Substring(1, ...
- RestFul是啥
1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...
- linux下源码安装mariadb
1.mariadb源码包下载地址:https://downloads.mariadb.org/ 2.安装mariadb是依赖包,创建mysql用户和目录: 命令 yum -y install rea ...
- C++回调函数、静态函数、成员函数踩过的坑。
C++回调函数.静态函数.成员函数踩过的坑. 明确一点即回调函数需要是静态函数.原因: 普通的C++成员函数都隐含了一个this指针作为参数,这样使得回调函数的参数和成员函数参数个数不匹配. 若不想使 ...
- [笔记]共享内存(shm)
一.特点 共享内存允许多个不同的进程可以访问同一块内存.相较于其他IPC形式,具有速度快,效率高的特点,共享内存的存在降低了在大规模数据处理过程中内存的消耗. 二.创建共享内存 1.头文件 #incl ...
- 给string定义一个扩展方法
创建一个 static 的类,并且里面的方法也必须是static的,第一个参数是被扩展的对象,必须标注为this,使用时,必须保证namespace using进来了. 实例: using Syste ...
- 如何阅读《JavaScript高级程序设计》(一)
题外话 最近在看<JavaScript高级程序设计>这本书,面对着700多页的厚书籍,心里有点压力,所以我决定梳理一下..探究一下到底怎么读这本书.本书的内容好像只有到ES5...所以只能 ...
- 了解dubbo+zookeeper
一.Dubbo是什么? Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,分布式服务框架(SOA),致力于提供高性能和透明化的RPC远程 ...
- tp5更改入口文件到根目录的方法分享
tp5把入口文件放到了public目录中,对于服务器或者vps来说没啥,因为可以指定目录,但是对于虚拟主机就不行了,我们必须吧index.php这入口文件放到根目录,那么我么需要改一下相对的引入文件的 ...