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

℡╲_俬逩灬. 提交于 2019-12-02 07:43:56

问题


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:

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

I want to remove all isbn nodes from this XMLdocument irrespective of its location.


回答1:


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>



回答2:


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;


来源:https://stackoverflow.com/questions/28103920/how-to-remove-a-xmlnode-from-xmldocument-occuring-at-multiple-nested-levels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!