删除每个子节点并保留注释

时间:2014-05-09 07:53:06

标签: c# xml

我有一个具有以下结构的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);

3 个答案:

答案 0 :(得分:1)

[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>

答案 1 :(得分:0)

使用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);
}

答案 2 :(得分:0)

在这里你可以使用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);


        }
    }
}