How to remove a XMLNode from XMLDocument occuring at multiple nested levels

后端 未结 2 649
既然无缘
既然无缘 2021-01-21 14:01

I have a XML which has a node which kind of gets repeated across multiple levels in the file using C#.

Example of the XML:


    

        
相关标签:
2条回答
  • 2021-01-21 14:09

    The easiest way is to use XDocument instead of XmlDocument. Use .Descendants() to find all Nodes of a specific name/type. Then .Remove() them.

    string xml = @"<books>
         <book>
             <title>The Walking Dead</title>
             <author>Test Name</author>
             <isbn>1239859895</isbn>
         </book>
         <book>
             <title>The Walking Dead</title>
             <author>
                 <isbn>29893893893</isbn>
                 <firstname>test1</firstname>
                 <lastname>test</lastname>
             </author>
         </book>        
     </books>";
    
    XDocument xdoc = XDocument.Parse(xml);
    xdoc.Descendants("isbn").Remove();
    string result = xdoc.ToString();
    

    But if you want to go with XmlDocument use this code:

     XmlDocument xmldoc = new XmlDocument();
     xmldoc.LoadXml(xml);
     foreach (var node in new  List<XmlNode>(xmldoc.GetElementsByTagName("isbn")
                                             .OfType<XmlNode>()).Where(
                                             x => x.ParentNode != null))
     {
         node.ParentNode.RemoveChild(node);                      
     }
    
     string result = xmldoc.OuterXml;
    
    0 讨论(0)
  • 2021-01-21 14:22

    As you indicated that you are working with an XmlDocument, you need to remove a child XmlElement node via the RemoveChild method on the parent node:

        string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
            <books>
                <book>
                    <title>The Walking Dead</title>
                    <author>Test Name</author>
                    <isbn>1239859895</isbn>
                </book>
                <book>
                    <title>The Walking Dead</title>
                    <author>
                        <isbn>29893893893</isbn>
                        <firstname>test1</firstname>
                        <lastname>test</lastname>
                    </author>
                </book>        
            </books>
            ";
        // Initialize and load the XmlDocument
        var doc = new XmlDocument();
        doc.LoadXml(xml);
    
        // Delete all XmlElements named "isbn".
        var list = doc.DocumentElement.GetElementsByTagName("isbn").OfType<XmlElement>().ToArray();
        foreach (var element in list)
        {
            var parent = element.ParentNode;
            if (parent != null)
                parent.RemoveChild(element);
        }
        var newXml = doc.OuterXml;
        Debug.WriteLine(newXml);
    

    And the output is:

    <?xml version="1.0" encoding="utf-16"?>
    <books>
      <book>
        <title>The Walking Dead</title>
        <author>Test Name</author>
      </book>
      <book>
        <title>The Walking Dead</title>
        <author>
          <firstname>test1</firstname>
          <lastname>test</lastname>
        </author>
      </book>
    </books>
    
    0 讨论(0)
提交回复
热议问题