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

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. pyspark中的dataframe的观察操作

    来自于:http://www.bubuko.com/infodetail-2802814.html 1.读取: sparkDF = spark.read.csv(path) sparkDF = spa ...

  2. js自定义事件CustomEvent、Event、TargetEvent

    1.Event Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 事件通常与函数结合使用,函数不会在事件发生前被执行! Event的事件都是系统自 ...

  3. ant-design-vue 修改组件样式

    /deep/ .ant-input { border-radius: 50px; }

  4. [CCTF] pwn350

    0x00: 之前打了CCTF,在CCTF的过程中遇到一个比较有意思的思路,记录一下. 0x01: 可以看到,这是一个 fmt 的漏洞,不过很简单,接收的输入都在stack中,可以确定输入在栈中的位置, ...

  5. Codevs 1213 解的个数(exgcd)

    1213 解的个数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知整数x,y满足如下面的条件: ax+by+c=0 p< ...

  6. AtCoder AGC004F Namori (图论)

    题目链接 https://atcoder.jp/contests/agc004/tasks/agc004_f 题解 神仙题.. 首先考虑树的情况,树是二分图,因此假设我们对二分图进行黑白染色,那么操作 ...

  7. java 根据html模板生成html文件

    1.代码部分 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test. ...

  8. 线程系列3--Java线程同步通信技术

    上一篇文章我们讲解了线程间的互斥技术,使用关键字synchronize来实现线程间的互斥技术.根据不同的业务情况,我们可以选择某一种互斥的方法来实现线程间的互斥调用.例如:自定义对象实现互斥(sync ...

  9. pytype

    与mypy相比不仅可以显示错误行数,还可以看到哪个函数错误. mypy的图 pytype的图

  10. CentOS7 docker服务部署

    以下命令可以在root身份下保存为shell脚本直接bash一次性执行 参考: https://yeasy.gitbooks.io/docker_practice/install/centos.htm ...