靓仔苏 發表於 2022-3-26 14:30:00

C# XML基础入门(XML文件内容增删改查清)

<h2>前言:</h2>
<p>  最近对接了一个第三方的项目,该项目的数据传输格式是XML。由于工作多年只有之前在医疗行业的时候有接触过少量数据格式是XML的接口,之后就几乎没有接触过了。因此对于XML这块自己感觉还是有很多盲点和不足的,所以自己通过一些网上的资料总结了一下XML相关知识点。</p>
<h2>什么是XML?</h2>
<ul>
<li>XML是一种可扩展标记语言(EXtensible Markup Language)。</li>
<li>XML是一种很像HTML的标记语言。</li>
<li>XML的设计宗旨是传输数据,而不是显示数据。</li>
<li>XML标签没有被预定义。您需要自行定义标签。</li>
<li>XML被设计为具有自我描述性。</li>
<li>XML是 W3C 的推荐标准。</li>
</ul>
<h2>XML的优缺点</h2>
<h3>XML的优点</h3>
<ul>
<li>语法严谨,格式统一,符合标准。</li>
<li>容易与其他系统进行远程交互,数据共享比较方便。</li>
</ul>
<h3>XML的缺点</h3>
<ul>
<li>扩展性、弹性、易读性均不佳。</li>
<li>XML文件庞大,文件格式复杂,传输占带宽。</li>
<li>服务器端和客户端解析XML花费较多的资源和时间。</li>
</ul>
<h2>XML简单示例</h2>
<p>在线xml验证工具:https://tool.ip138.com/xml/</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;books&gt;
    &lt;book&gt;
      &lt;author&gt;追逐时光者&lt;/author&gt;
      &lt;title&gt;XML学习教程&lt;/title&gt;
      &lt;publisher&gt;时光出版社&lt;/publisher&gt;
    &lt;/book&gt;
&lt;/books&gt;
</pre>
</div>
<h2 id="articleContentId" class="title-article">XML中5个预定义的实体引用</h2>
<table style="height: 205px; width: 600px">
<thead>
<tr><th>转义字符</th><th>符号</th><th>名称</th></tr>
</thead>
<tbody>
<tr>
<td><code>&amp;amp;</code></td>
<td>&amp;</td>
<td>和号</td>
</tr>
<tr>
<td><code>&amp;lt;</code></td>
<td>&lt;</td>
<td>小于</td>
</tr>
<tr>
<td><code>&amp;gt;</code></td>
<td>&gt;</td>
<td>大于</td>
</tr>
<tr>
<td><code>&amp;apos;</code></td>
<td>'</td>
<td>省略号</td>
</tr>
<tr>
<td><code>&amp;quot;</code></td>
<td>"</td>
<td>引号</td>
</tr>
</tbody>
</table>
<blockquote>
<p>严格地讲,在 XML 中仅有字符 "&lt;"和"&amp;" 是非法的。省略号、引号和大于号是合法的。这时,Xml有两种解决方案来处理这种问题。</p>
<ol>
<li>CDATA</li>
<li>转义字符</li>
</ol></blockquote>
<h3>C#把特殊符号转换为转义字符</h3>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 特殊符号转换为转义字符
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="xmlStr"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> XmlSpecialSymbolConvert(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> xmlStr)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> xmlStr.Replace(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&amp;</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&amp;amp;</span><span style="color: rgba(128, 0, 0, 1)">"</span>).Replace(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&amp;lt;</span><span style="color: rgba(128, 0, 0, 1)">"</span>).Replace(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&gt;</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&amp;gt;</span><span style="color: rgba(128, 0, 0, 1)">"</span>).Replace(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\'</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&amp;apos;</span><span style="color: rgba(128, 0, 0, 1)">"</span>).Replace(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\"</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">&amp;quot;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      }</span></pre>
</div>
<h2>C#创建简单的XML文件  </h2>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 创建Xml文件
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> CreateXmlFile()
      {
            XmlDocument xmlDoc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XmlDocument();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建类型声明节点</span>
            XmlNode node = xmlDoc.CreateXmlDeclaration(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1.0</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">);
            xmlDoc.AppendChild(node);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建Xml根节点</span>
            XmlNode root = xmlDoc.CreateElement(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">books</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            xmlDoc.AppendChild(root);

            XmlNode root1 </span>= xmlDoc.CreateElement(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">book</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            root.AppendChild(root1);

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建子节点</span>
            CreateNode(xmlDoc, root1, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">追逐时光者</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            CreateNode(xmlDoc, root1, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">XML学习教程</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            CreateNode(xmlDoc, root1, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">publisher</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">时光出版社</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将文件保存到指定位置</span>
            xmlDoc.Save(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D://xmlSampleCreateFile.xml</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>   
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 创建节点   
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>   
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="xmlDoc"&gt;</span><span style="color: rgba(0, 128, 0, 1)">xml文档</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>   
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="parentNode"&gt;</span><span style="color: rgba(0, 128, 0, 1)">Xml父节点</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>   
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="name"&gt;</span><span style="color: rgba(0, 128, 0, 1)">节点名</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>   
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="value"&gt;</span><span style="color: rgba(0, 128, 0, 1)">节点值</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>   
      <span style="color: rgba(128, 128, 128, 1)">///</span>   
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> CreateNode(XmlDocument xmlDoc, XmlNode parentNode, <span style="color: rgba(0, 0, 255, 1)">string</span> name, <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> value)
      {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建对应Xml节点元素</span>
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
            node.InnerText </span>=<span style="color: rgba(0, 0, 0, 1)"> value;
            parentNode.AppendChild(node);
      }</span></pre>
</div>
<h3>创建生成的Xml文件</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="utf-8"</span><span style="color: rgba(0, 0, 255, 1)">?&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">books</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">book</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>追逐时光者<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>XML学习教程<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">publisher</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>时光出版社<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">publisher</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">book</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">books</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h2>C#在XML文件添加节点</h2>
<p>这次我们是在上一次新建的XML文件中做操作,在book二级节点下添加一个新的节点名为publishdate(发布时间),该节点的value为2022-03-26。</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> AppendNode()
      {
            XmlDocument xmlDoc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XmlDocument();
            xmlDoc.Load(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D://xmlSampleCreateFile.xml</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载Xml文件</span>
            XmlNode root = xmlDoc.SelectSingleNode(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">books/book</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">选择要添加子节点的book节点
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个新的Xml节点元素</span>
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">publishdate</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
            node.InnerText </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2022-03-26</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            root.AppendChild(node);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将创建的item子节点添加到items节点的尾部</span>
            xmlDoc.Save(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D://AppendNodeFile.xml</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">保存修改的Xml文件内容</span>
      }</pre>
</div>
<h3>添加节点成功后的XML文件内容</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="utf-8"</span><span style="color: rgba(0, 0, 255, 1)">?&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">books</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">book</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>追逐时光者<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>XML学习教程<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">publisher</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>时光出版社<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">publisher</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">publishdate</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>2022-03-26<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">publishdate</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">book</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">books</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h2>C#修改XML文件节点的数据</h2>
<p>这次我们是在第一次新建的XML文件中做操作,把在book二级节点下的author的内容改成:大姚同学</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> UpdateXml()
      {
            XmlDocument xmlDoc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XmlDocument();
            xmlDoc.Load(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D://xmlSampleCreateFile.xml</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载Xml文件</span>
            XmlNode xns = xmlDoc.SelectSingleNode(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">books/book</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查找要修改的节点</span>
            XmlNodeList xmlNodeList = xns.ChildNodes;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取出book节点下所有的子节点</span>

            <span style="color: rgba(0, 0, 255, 1)">foreach</span> (XmlNode xmlNode <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> xmlNodeList)
            {
                XmlElement xmlElement </span>= (XmlElement)xmlNode;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将节点转换一下类型</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span> (xmlElement.Name==<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(128, 0, 0, 1)">"</span>)<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断该子节点是否是要查找的节点</span>
<span style="color: rgba(0, 0, 0, 1)">                {
                  xmlElement.InnerText </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">大姚同学</span><span style="color: rgba(128, 0, 0, 1)">"</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置新值</span>
                  <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                }
            }
            xmlDoc.Save(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D://UpdateXml.xml</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">保存修改的Xml文件内容</span>
      }</pre>
</div>
<h3>修改后的XML文件内容</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="utf-8"</span><span style="color: rgba(0, 0, 255, 1)">?&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">books</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">book</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>大姚同学<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>XML学习教程<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">publisher</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>时光出版社<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">publisher</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">book</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">books</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h2>C#删除XML文件中的指定节点</h2>
<p>这次我们是在第一次新建的XML文件中做操作,删除author节点。</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> DeleteXmlNode()
      {
            XmlDocument xmlDoc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XmlDocument();
            xmlDoc.Load(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D://xmlSampleCreateFile.xml</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载Xml文件</span>
            XmlNode xns = xmlDoc.SelectSingleNode(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">books/book</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查找要删除的根节点</span>

            <span style="color: rgba(0, 0, 255, 1)">#region</span> 删除author节点

            <span style="color: rgba(0, 0, 255, 1)">var</span> delNode = xmlDoc.SelectSingleNode(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">books/book/</span><span style="color: rgba(128, 0, 0, 1)">"</span> + <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            xns.RemoveChild(delNode);

            </span><span style="color: rgba(0, 0, 255, 1)">#endregion</span><span style="color: rgba(0, 0, 0, 1)">

            xmlDoc.Save(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D://DeleteXmlNode.xml</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">保存操作后的Xml文件内容</span>
      }</pre>
</div>
<h2>C#清空指定XML节点数据</h2>
<p>这次我们是在第一次新建的XML文件中做操作,清空author节点下的数据。</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> ClearDataXmlNode()
      {
            XmlDocument xmlDoc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XmlDocument();
            xmlDoc.Load(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D://xmlSampleCreateFile.xml</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载Xml文件</span>
            XmlNode xns = xmlDoc.SelectSingleNode(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">books/book</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查找要删除的根节点</span>

            <span style="color: rgba(0, 0, 255, 1)">#region</span> 清空author节点下的数据<span style="color: rgba(0, 0, 0, 1)">
            XmlNodeList xmlNodeList </span>= xns.ChildNodes;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取出book节点下所有的子节点</span>
            <span style="color: rgba(0, 0, 255, 1)">foreach</span> (XmlNode xmlNode <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> xmlNodeList)
            {
                XmlElement xmlElement </span>= (XmlElement)xmlNode;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将节点转换一下类型</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span> (xmlElement.Name == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">author</span><span style="color: rgba(128, 0, 0, 1)">"</span>)<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断该子节点是否是要查找的节点</span>
<span style="color: rgba(0, 0, 0, 1)">                {
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清空author节点下的数据</span>
                  xmlElement.RemoveAll();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除该节点的全部内容</span>
<span style="color: rgba(0, 0, 0, 1)">                }
            }
            </span><span style="color: rgba(0, 0, 255, 1)">#endregion</span><span style="color: rgba(0, 0, 0, 1)">

            xmlDoc.Save(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D://ClearDataXmlNode.xml</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">保存操作后的Xml文件内容</span>
      }</pre>
</div>
<h2>学习参考资料</h2>
<p>w3cSchool-XML 教程</p>
<p>.NET中XML序列化和反序列化常用类和用来控制XML序列化的属性总结</p>
<div id="gtx-trans" style="position: absolute; left: -17px; top: 1374.28px">&nbsp;</div>

</div>
<div id="MySignature" role="contentinfo">
    <blockquote >
<p style='font-family:YouYuan;font-size: 16px;margin: 0 auto 0.01em auto;'><span style='font-size: 17px; '>作者名称:</span>追逐时光者</p>
<p style='font-family:YouYuan;font-size: 16px;margin: 0 auto 0.01em auto;'><span style='font-size: 17px; '>作者简介:</span>一个热爱编程、善于分享、喜欢学习、探索、尝试新事物和新技术的全栈软件工程师。</p>
<p style='font-family:YouYuan;font-size: 16px;margin: 0 auto 0.01em auto;'>
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。如果该篇文章对您有帮助的话,可以点一下右下角的【&hearts;推荐&hearts;】,希望能够持续的为大家带来好的技术文章,文中可能存在描述不正确的地方,欢迎指正或补充,不胜感激。
</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/Can-daydayup/p/16036872.html
頁: [1]
查看完整版本: C# XML基础入门(XML文件内容增删改查清)