I am trying to remove attributes from an xml
whose value contains certain string.
This is the code I wrote,
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='#novel' ISBN='1-861001-57-5'>" +
"<title id ='#x'>Pride And Prejudice</title>" +
"</book>");
//Get the root element of the element
XmlElement el = doc.DocumentElement;
foreach (XmlElement a in doc.ChildNodes)
{
XmlAttributeCollection atributos = a.Attributes;
foreach (XmlAttribute att in atributos)
{
if (att.Value.StartsWith("#"))
{
a.RemoveAttribute(att.Name); //Gives invalid operation exception
}
}
}
In the second iteration, it gives me invalid operation error.
I looked around and found something that it could be because I am trying to modify the collection in the foreach loop.
If that is the case, what will be the best solution?
I generate an new xml?