Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Deleting Node

Status
Not open for further replies.

prrm333

Programmer
Apr 14, 2003
97
US
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?
 
Is there any short of names that make you use names of variables so close to property/method's name?! In any case, this.
[tt]
XmlDocument xmlDoc = new XmlDocument( );
xmlDoc.Load(fileName);
string sDate = "2008/10";
XmlNodeList parentNodes = xmlDoc.GetElementsByTagName("Month");
for (int j=parentNodes.Count-1;j>=0;j--)
{
for(int i = parentNodes[j].ChildNodes.Count - 1; i >= 0; i--)
{
XmlNode childNode = parentNodes[j].ChildNodes;
if (childNode.Name == "Date" && childNode.InnerXml == sDate)
{
parentNodes[j].ParentNode.RemoveChild(parentNodes[j]);
}
}
}
xmlDoc.Save(fileName);
[/tt]
There are so many subtle differences that I eventually do not bother to point out individually. And I do not imply the above is the best approach.
 
Thanks, that did the trick. I can see some of the differences although when I tried to make similar changes I kept getting errors. I'm not sure what you refer to with using property/method names. The actual date will be selected by the user and the the only other 2 are the XML tags. Anyway thanks for the help.
 
I made an attempt to modify your code as an error occured if only one record was left.

This is my modification:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(_file);
XmlNodeList parentNodes = xmlDoc.GetElementsByTagName("Month");
{
for (int i = parentNodes[j].ChildNodes.Count - 1; i >= 0; i--)
{
XmlNode childNode = parentNodes[j].ChildNodes;
if (j > 0 && childNode.Name == "Date" && childNode.InnerXml == sDate)
{
parentNodes[j].ParentNode.RemoveChild(parentNodes[j]);
xmlDoc.Save(filename);
}
else if (j == 0 && childNode.Name == "Date" && childNode.InnerXml == sDate)
{
DialogResult dr = MessageBox.Show("You have only one month saved in your data file.\n" +
"If you want to delete this record you must delete the entire file. " +
"Press \"Okay\" to delete or \"Cancel\" to keep the file\"", "Delete File, Yes or No?", MessageBoxButtons.OKCancel);
{
if (dr == DialogResult.OK)
File.Delete(filename);
}
}
}
}

It works correctly except in the situation where a user tries to delete the most recent date with older dates already in the file.
 
It is told that under such and such conditions will make the "your code" erroneous etc etc. I might take the word at its face value and I might be willing to look into it if only I can make any sense out of the last post. For instance, what is j? If that's a mistake, can one take the word at its face value?
 
I think I solved the problem by looking at the first for loop (i). If this is equal to 1 then there is only one row in the file. I'm not quite sure what you are saying in that I only added to your code which worked correctly. The only exception was that there was no handling of the file having no records in it but still existing. Either way I think I have it solved with your original code plus testing whether or not the first loop has only one record or not. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top