所有代码都在同一个类中,含有对象

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来读写的更多相关文章

  1. .net学习笔记---xml操作及读写

    一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...

  2. C#操作XML的完整例子——XmlDocument篇

    这是一个用c#控制台程序下,  用XmlDocument 进行XML操作的的例子,包含了查询.增加.修改.删除.保存的基本操作.较完整的描述了一个XML的整个操作流程.适合刚入门.net XML操作的 ...

  3. C#操作XML的完整例子——XmlDocument篇(转载,仅做学习之用)

    原文地址:http://www.cnblogs.com/serenatao/archive/2012/09/05/2672621.html 这是一个用c#控制台程序下,  用XmlDocument 进 ...

  4. 对xml操作

    已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...

  5. 简单的XML操作类

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  6. C#常用操作类库三(XML操作类)

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  7. XML Helper XML操作类

    写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web ...

  8. .Net XML操作 <第二篇>

    一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...

  9. C#操作Xml:linq to xml操作XML

    LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...

  10. c#XML操作类的方法总结

    using System.Xml;using System.Data; namespace DotNet.Utilities{    /// <summary>    /// Xml的操作 ...

随机推荐

  1. JavaScript 中 console 的用法 -------------------引用

    1.在调试 JS 代码时,很多人倾向于使用 alert() 或者 console.log() 方法来输出信息,正如某些 Java 程序员喜欢在调试代码时使用System.out.println() 输 ...

  2. [人物存档]【AI少女】【捏脸数据】金发西洋风格

    点击下载(城通网盘):AISChaF_20191103124436239.png

  3. jquery fadeIn()方法 语法

    jquery fadeIn()方法 语法 作用:fadeIn() 方法使用淡入效果来显示被选元素,假如该元素是隐藏的.大理石平台检定规程 语法:$(selector).fadeIn(speed,cal ...

  4. wind本地MySQL数据到hive的指定路径,Could not create file

    一:使用:kettle:wind本地MySQL数据到hive的指定路径二:问题:没有root写权限网上说的什么少jar包,我这里不存在这种情况,因为我自己是导入jar包的:mysql-connecto ...

  5. 2018第九届蓝桥杯C/C++ A组试题答案参考

    题目1 标题:分数 1/1 + 1/2 + 1/4 + 1/8 + 1/16 + .... 每项是前一项的一半,如果一共有20项,求这个和是多少,结果用分数表示出来.类似:3/2当然,这只是加了前2项 ...

  6. 使用A* Pathfinding Project的一些心得

    最近在游戏开发中要做寻路.首选果断就是Unity3D自带的寻路啦.方便稳定,基本功能都能满足.我们的需求也不复杂,就是一个英雄在不同的地图中探索.但是介于一个比较恶心的问题,果断放弃了它.所以,说A* ...

  7. BeetleX之快速构建Web多房间聊天室

    其实构建一个Web多房间聊天室也并不是什么困难的技术,借助于websocket就可以轻松实现多用户在线实时通讯交互:在这里主要介绍一下在BeetleX和BeetleXjs的支持下如何让这个功能实现的更 ...

  8. SQL语句中 NOT IN 子句的“正确打开方式”

    在写SQL语句的时候,若where条件是判断用户不在某个集合当中,我们习惯使用 where 列名 not in (集合) 子句,这种写法本身没有问题,但实践过程中却发现很多人在写类似的SQL语句时,写 ...

  9. 2.ibatis执行流程解析

    以下仅为个人理解,如有问题,欢迎指正

  10. hotspot 线程状态