删除所有子节点并保留注释

本文关键字:保留 注释 子节点 删除 | 更新日期: 2023-09-27 18:02:35

我有一个结构如下的xml:

<?xml version="1.0" encoding="utf-8"?>
<attributes>
<!--
    Attribute mapping file defines mapping between AVEVA PID attribute name and        corresponding 
AVEVA NET Characteristic name.-->
<!-- Don't output off-sheet pipe connector characteristics to the EIWM registry file   <drawing>_avngate.xml -->
<attribute class="PIPE CONNECTION FLAGBACKWARD" from="Grid_Reference" output="false"/>
<attribute class="PIPE CONNECTION FLAGBACKWARD" from="PipeId" output="false"/>
<attribute class="All" from="TagSuffix" output="false"/>
</attributes>

我想删除所有子节点并保留评论。下面是我的代码:

XmlDocument myxml = new XmlDocument();
myxml.Load(filePath);
XmlNode lastChild;
XmlNode root = myxml.DocumentElement;
while ((lastChild = root.LastChild) != null)
{
    root.RemoveChild(lastChild);
}
myxml.Save(filePath);

删除所有子节点并保留注释

[Fact]
public void Test()
{
    var root = XElement.Load("Data.xml");
    root.DescendantNodes()
        .Where(x => x.NodeType != XmlNodeType.Comment)
        .ToList()
        .ForEach(x => x.Remove());
    root.Save("Data.xml");
}

结果输出:

<?xml version="1.0" encoding="utf-8"?>
<attributes>
  <!--
    Attribute mapping file defines mapping between AVEVA PID attribute name and        corresponding 
AVEVA NET Characteristic name.-->
  <!-- Don't output off-sheet pipe connector characteristics to the EIWM registry file   <drawing>_avngate.xml -->
</attributes>

如何使用Linq to xml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XElement xElement = XElement.Parse(@"<?xml version=""1.0"" encoding=""utf-8""?>
<attributes>
<!--
    Attribute mapping file defines mapping between AVEVA PID attribute name and        corresponding 
AVEVA NET Characteristic name.-->
<!-- Don't output off-sheet pipe connector characteristics to the EIWM registry file   <drawing>_avngate.xml -->
<attribute class=""PIPE CONNECTION FLAGBACKWARD"" from=""Grid_Reference"" output=""false""/>
<attribute class=""PIPE CONNECTION FLAGBACKWARD"" from=""PipeId"" output=""false""/>
<attribute class=""All"" from=""TagSuffix"" output=""false""/>
</attributes>");
            var fstElement = xElement.FirstNode;
            int i = 0; 
            do
            {
                if (fstElement.NodeType != XmlNodeType.Comment)
                {
                    if (fstElement.Parent != null)
                    {
                        fstElement.Remove();
                        i--;       
                    }
                }
                i++;  
            } while ( i< xElement.Nodes().Count() && (fstElement = xElement.Nodes().ToArray()[i]) != null);

            Console.WriteLine(xElement);

        }
    }
}

使用XmlNode。NodeType,保存需要删除的节点,删除后再删除。

ArrayList<XmlNode> nodesToDelete = new ArrayList<XmlNode>();
while ((lastChild = root.LastChild) != null)
{
    if (lastChild.NodeType != XmlNodeType.Comment)
        nodesToDelete.Add(lastChild);
}
foreach (XmlNode node in nodesToDelete)
{
    if (node.ParentNode != null)
        root.RemoveChild(node);
}