I have the following in an XML file:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Month>
<Date>2008/12</Date>
<StBal>$0.00</StBal>
<TDep>$0.00</TDep>
<StTDep>$0.00</StTDep>
<TWith>$0.00</TWith>
<GTot>$0.00</GTot>
</Month>
<Month>
<Date>2008/11</Date>
<StBal>$0.00</StBal>
<TDep>$0.00</TDep>
<StTDep>$0.00</StTDep>
<TWith>$0.00</TWith>
<GTot>$0.00</GTot>
</Month>
<Month>
<Date>2008/10</Date>
<StBal>$0.00</StBal>
<TDep>$0.00</TDep>
<StTDep>$0.00</StTDep>
<TWith>$0.00</TWith>
<GTot>$0.00</GTot>
</Month>
</Data>
Using the following code I am able to only delete on child node:
XmlDocument xmlDoc = new XmlDocument( );
xmlDoc.Load(fileName);
string sDate = "2008/10";
XmlNodeList parentNodes = xmlDoc.GetElementsByTagName "Data");
foreach(XmlNode parentNode in parentNodes)
{
for(int i = parentNode.ChildNodes.Count - 1; i >= 0; i--)
{
XmlNode childNode = parentNode.ChildNodes;
if (parentNode.Name == "Month" && childNode.InnerXml == sDate)
{
parentNode.RemoveChild(childNode);
}
}
}
xmlDoc.Save(fileName);
I would like to delete the entire <Month><Month> section for the specified date. When I try this nothing happens. It had previously only deleted the specified <Date></Date> section.
Any ideas?
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Month>
<Date>2008/12</Date>
<StBal>$0.00</StBal>
<TDep>$0.00</TDep>
<StTDep>$0.00</StTDep>
<TWith>$0.00</TWith>
<GTot>$0.00</GTot>
</Month>
<Month>
<Date>2008/11</Date>
<StBal>$0.00</StBal>
<TDep>$0.00</TDep>
<StTDep>$0.00</StTDep>
<TWith>$0.00</TWith>
<GTot>$0.00</GTot>
</Month>
<Month>
<Date>2008/10</Date>
<StBal>$0.00</StBal>
<TDep>$0.00</TDep>
<StTDep>$0.00</StTDep>
<TWith>$0.00</TWith>
<GTot>$0.00</GTot>
</Month>
</Data>
Using the following code I am able to only delete on child node:
XmlDocument xmlDoc = new XmlDocument( );
xmlDoc.Load(fileName);
string sDate = "2008/10";
XmlNodeList parentNodes = xmlDoc.GetElementsByTagName "Data");
foreach(XmlNode parentNode in parentNodes)
{
for(int i = parentNode.ChildNodes.Count - 1; i >= 0; i--)
{
XmlNode childNode = parentNode.ChildNodes;
if (parentNode.Name == "Month" && childNode.InnerXml == sDate)
{
parentNode.RemoveChild(childNode);
}
}
}
xmlDoc.Save(fileName);
I would like to delete the entire <Month><Month> section for the specified date. When I try this nothing happens. It had previously only deleted the specified <Date></Date> section.
Any ideas?