How to remove an XmlNode from XmlNodeList

前端 未结 6 1521
耶瑟儿~
耶瑟儿~ 2020-12-06 04:09

I need to remove an XmlNode based on a condition. How to do it?

foreach (XmlNode drawNode in nodeList)
{
       //Based on a condition
       drawNode.Remove         


        
相关标签:
6条回答
  • 2020-12-06 04:43

    You can not easily use iterators (foreach-statement) to delete your nodes. As I see it you can do the following:

    1) In your foreach-loop, save a list of all elements that should be removed. Then you loop through just those elements and remove them.

    2) Use an ordinary for-loop and keep track of which is the next element to visit after you have deleted one item.

    Edit: When using a for-loop do it the way Fredrik suggests, looping backwards.

    0 讨论(0)
  • 2020-12-06 04:43

    Isn't the following a little simpler:

    XmlDocument doc = new XmlDocument(); 
    doc.Load(fileName); 
    XmlNodeList nodes = doc.SelectNodes("some-xpath-query"); 
    while (nodes.FirstChild != null) {     
        nodes.RemoveChild(nodes.FirstChild); 
    } 
    doc.Save(fileName); 
    
    0 讨论(0)
  • 2020-12-06 04:51

    If you're trying to remove a node from the XML DOM, this isn't the correct way. Because an XMLNodeList is just a list of nodes. Rather you want to remove the node from the parent node. Like this:

    XmlNode parentNode = // some value
    XmlNode drawNode = // some value
    parentNode.ParentNode.RemoveChild(drawNode);
    
    0 讨论(0)
  • 2020-12-06 04:54

    Seems to me you're trying to just remove an entire XML Element...

    If this is your XML...

    <Xml1>
      <body>
        <Book>
          <Title name="Tom Sawyer" />
          <Author value="Mark Twain" />
        </Book>
        <Book>
          <Title name="A Tale of Two Cities" />
          <Author value="Charles Dickens" />
        </Book>
      </body>
    </Xml1>
    

    If you wanted to delete a book, you need to grab the first <Book> node. You can do that with:

    XmlDocument doc = new XmlDocument();
    doc.Load(fileName);
    
    XmlNodeList nodes = doc.GetElementsByTagName("body");
    XmlNode bodyNode = nodes[0];
    XmlNode firstBook = bodyNode.ChildNodes[0];
    

    Once you have the "first book" node, you can delete that from the body node using:

    bodyNode.RemoveChild(firstBook);
    

    This will automatically affect/update the XML Document variable, so doc will now only have:

    <Xml1>
      <body>
        <Book>
          <Title name="A Tale of Two Cities" />
          <Author value="Charles Dickens" />
        </Book>
      </body>
    </Xml1>
    

    If you want to grab and delete the whole body, you should be able to do:

    XmlNodeList xml1 = doc.GetElementsByTagName("Xml1");
    XmlNode xmlNode = xml[0];
    xmlNode.RemoveChild(bodyNode);
    

    And the doc variable will be updated to no longer contain the body element, and can then be resaved to the file system:

    doc.Save(fileName);
    

    Where fileName is the full path to the XML Document on your computer.

    Best of all, we aren't using doc.SelectNodes(), so we don't need to worry about using an XMLNamespaceManager.

    0 讨论(0)
  • 2020-12-06 04:58
        XmlNodeList xnodeContact = xmldocContact.GetElementsByTagName("contact");
              foreach (ListViewItem item in listViewContacts.Items)
                {
                    if (item.Checked)
                    {
                        if (item.Index >= 0)
                            xnodeContact[0].ParentNode.RemoveChild(xnodeContact[0]);
                            listViewContacts.Items.Remove(item);
                        }
                    }
                }
                xmldocContact.Save(appdataPath + "\\WhatAppcontactList.xml");
                Invalidate();
    
    0 讨论(0)
  • 2020-12-06 04:59

    This should do the trick for you:

    for (int i = nodeList.Count - 1; i >= 0; i--)
    {
        nodeList[i].ParentNode.RemoveChild(nodeList[i]);
    }
    

    If you loop using a regular for-loop, and loop over it "backwards" you can remove items as you go.

    Update: here is a full example, including loading an xml file, locating nodes, deleting them and saving the file:

    XmlDocument doc = new XmlDocument();
    doc.Load(fileName);
    XmlNodeList nodes = doc.SelectNodes("some-xpath-query");
    for (int i = nodes.Count - 1; i >= 0; i--)
    {
        nodes[i].ParentNode.RemoveChild(nodes[i]);
    }
    doc.Save(fileName);
    
    0 讨论(0)
提交回复
热议问题