When trying to delete multiple sections, I could only get one to delete at a time. I found bugs in XmlConfigSource and DotNetConfigSource in how it was going through and removing nodes. This affected removing sections or keys. Below are the updated methods for XmlConfigSource. Similar changes need to be made to the same methods in DotNetConfigSource. I did submit a bug report on this.
/// <summary>
/// Removes all XML sections that were removed as configs.
/// </summary>
private void RemoveSections()
{
XmlAttribute attr = null;
XmlNode node;
for (int i = configDoc.DocumentElement.ChildNodes.Count - 1; i > -1; i--) {
node = configDoc.DocumentElement.ChildNodes.Item(i);
if (keyName != null) {
if (this.Configs[sectionName].Get(keyName.Value) == null) {
sectionNode.RemoveChild(node);
}
} else {
throw new ArgumentException("Name attribute not found in key");
}
}
}
}
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
When trying to delete multiple sections, I could only get one to delete at a time. I found bugs in XmlConfigSource and DotNetConfigSource in how it was going through and removing nodes. This affected removing sections or keys. Below are the updated methods for XmlConfigSource. Similar changes need to be made to the same methods in DotNetConfigSource. I did submit a bug report on this.
/// <summary>
/// Removes all XML sections that were removed as configs.
/// </summary>
private void RemoveSections()
{
XmlAttribute attr = null;
XmlNode node;
for (int i = configDoc.DocumentElement.ChildNodes.Count - 1; i > -1; i--) {
node = configDoc.DocumentElement.ChildNodes.Item(i);
if (node.NodeType == XmlNodeType.Element && node.Name == "Section") {
attr = node.Attributes["Name"];
if (attr != null) {
if (this.Configs[attr.Value] == null) {
configDoc.DocumentElement.RemoveChild(node);
}
} else {
throw new ArgumentException("Section name attribute not found");
}
}
}
}
/// <summary>
/// Removes all XML keys that were removed as config keys.
/// </summary>
private void RemoveKeys(string sectionName)
{
XmlNode sectionNode = GetSectionByName(sectionName);
XmlAttribute keyName = null;
XmlNode node;
if (sectionNode != null) {
for (int i = sectionNode.ChildNodes.Count - 1; i > -1; i--) {
node = sectionNode.ChildNodes.Item(i);
if (node.NodeType == XmlNodeType.Element && node.Name == "Key") {
keyName = node.Attributes["Name"];
if (keyName != null) {
if (this.Configs[sectionName].Get(keyName.Value) == null) {
sectionNode.RemoveChild(node);
}
} else {
throw new ArgumentException("Name attribute not found in key");
}
}
}
}
}