Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<hints>
  <hint>
    <id value="4">
    <clues value="A Bank and Coffee Shop">
    <answer value="Starbucks">
    <difficulty value="4">
    <date value="09/16/21">
  <hint>
<hint>


What I have tried:

C#
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
    string xmlPath = "clueHints.xml";
    XmlDocument doc = new XmlDocument();
    doc.Load(xmlPath);
    XmlNodeList newXMLNodes = doc.GetElementsByTagName("ID");
    string value = txtClues.Text;
    foreach (XmlNode node in newXMLNodes)
    {
        foreach (XmlAttribute attribute in node.Attributes)
        {
            if(attribute.Value == value)
            {
                node.ParentNode.RemoveAll();
                MessageBox.Show("Hint Deleted");
            }
            newXMLNodes.GetEnumerator();
            doc.Save("clueHints.xml");
        }
    }
}
Posted
Updated 28-Sep-21 22:10pm
v2
Comments
Richard MacCutchan 27-Sep-21 15:02pm    
You should not delete items while enumerating a list as it destroys the set.
Sharad Ranpara 2021 27-Sep-21 16:35pm    
What's the best way to do it?

I Do not understand the solution below.
Richard MacCutchan 28-Sep-21 3:37am    
As OriginalGriff suggests, you need to build a 'deletelist'. Create a new empty list, and as you iterate over the XML, every time you encounter an item that needs to be deleted make a note of it in the new list. Then when you have completed the iteration you should have a list of items that are to be deleted. You can now go iterate over the new list and use the references to delete the items from the XML one by one without this problem.

My suggestion is to use use LINQ to XML. It greatly simplifies the problem. Given a XML file like this

<?xml version="1.0" encoding="utf-8" ?>
<hints>
  <hint>
    <id value="1"/>
    <clues value="A"/>
    <answer value="Starbucks"/>
    <difficulty value="4"/>
    <date value="09/16/21"/>
  </hint>
  <hint>
    <id value="2"/>
    <clues value="B"/>
    <answer value="Starbucks"/>
    <difficulty value="4"/>
    <date value="09/16/21"/>
  </hint>
  <hint>
    <id value="3"/>
    <clues value="C"/>
    <answer value="Starbucks"/>
    <difficulty value="4"/>
    <date value="09/16/21"/>
  </hint>
  <hint>
    <id value="4"/>
    <clues value="D"/>
    <answer value="Starbucks"/>
    <difficulty value="4"/>
    <date value="09/16/21"/>
  </hint>
  <hint>
    <id value="3"/>
    <clues value="E"/>
    <answer value="Starbucks"/>
    <difficulty value="4"/>
    <date value="09/16/21"/>
  </hint>
</hints>


You can remove all the hints with an id of 3 like this

C#
public void TestXml()
 {
     string xmlPath = @"C:\temp\hints.xml";
     XDocument document = XDocument.Load(xmlPath);
     string targetId = "3";
     document.Descendants("id")
         .Where((e) => e.FirstAttribute.Value == targetId)
         .Select((e) => e.Parent)
         .Remove();
     document.Save(@"C:\temp\hints2.xml");
 }

 
Share this answer
 
You cannot remove elements from any collection while iterating over it: if you try, you will get this error.

It's a bit like trying to count a pile of coins while someone is randomly adding and subtracting money. It's far too easy to get your count confused - and so does the iterator when you try to change the collection: it doesn't know which element should be next, so it throws an exception.

Instead, build a separate collection of items to remove, and then delete them separately when the list is complete.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900