I need a function that can remove all child nodes of some specific node. But only child nodes, not an attributes also. There is standard function in System.XML to remove all childs but it also removes all attributes that I have.
In result I write my own funcrtion that takes as parameters xmlDocument, my parent node (sector), and bool variable toRemoveAttributes. In this case I takes all attributes to one XmlAttributeCollection and then use RemoveAll function.
public void RemoveChild(XmlDocument xd, string sectorName, bool removeAttributes)
{
XmlElement sector;
if (sectorName == "root")
sector = xd.DocumentElement;
else
sector = (XmlElement)xd.GetElementsByTagName(sectorName)[0];
XmlAttributeCollection atr = sector.Attributes;
sector.RemoveAll();
if(!removeAttributes)
{
for (int i = 0; i < atr.Count; i++)
sector.SetAttribute(atr[i].Name, atr[i].Value);
}
}
In result my attributes is still removed. When I debugged my code i saw that after RemoveAll() also everything is deleted from my 'atr' colection.