PC Review


Reply
Thread Tools Rate Thread

am I deleting correctly?

 
 
=?Utf-8?B?bWVsYW5pZWFi?=
Guest
Posts: n/a
 
      23rd Sep 2005
Hi,
I'm deleting nodes in my xml file, and it does seem to work, but then when I
later reload the file and make an xmlNodeList, the nodelist count still
includes the deleted nodes yet the file shows no sign of them. Here is what
I have:

Example of my xml:
<Catalog>
<File ID = "1">
<Customer>Me</Customer>
<Date>Today</Date>
 
Reply With Quote
 
 
 
 
Kai Brinkmann [Microsoft]
Guest
Posts: n/a
 
      23rd Sep 2005
XmlNode.RemoveAll() removes all *child nodes* of the node (including
attributes) but *not* the node itself. Are you sure there is no sign of the
node in the saved XML file? I would expect the file to look like this after
your code runs:

<Catalog>
<File ID = "1">
<Customer>Me</Customer>
<Date>Today</Date>
 
Reply With Quote
 
=?Utf-8?B?bWVsYW5pZWFi?=
Guest
Posts: n/a
 
      23rd Sep 2005
Thanks. Yes, the <File></File> was in there, I just didn't notice it through
all the data. And I'll definitely give the xpath a shot.
Thanks again,
Mel

"Kai Brinkmann [Microsoft]" wrote:

> XmlNode.RemoveAll() removes all *child nodes* of the node (including
> attributes) but *not* the node itself. Are you sure there is no sign of the
> node in the saved XML file? I would expect the file to look like this after
> your code runs:
>
> <Catalog>
> <File ID = "1">
> <Customer>Me</Customer>
> <Date>Today</Date>
> .
> .
> .
> </File>
> <File />
> </Catalog>
>
> The empty <File /> node corresponds to <File ID="2"> with all attributes and
> child nodes deleted.
>
> Try replacing your delete statement with this:
>
> nl.Item[i].ParentNode.RemoveChild(nl.Item[i]);
>
> BTW, you could simplify your code somewhat using XPath expressions. Since
> you know up front which node you would like to delete, there is really no
> need to loop through all file nodes. You could simply do this:
>
> XmlNode node = xml.SelectSingleNode("//File[@ID='2']");
> node.ParentNode.RemoveChild(node);
>
> The first line selects the first file node anywhere with an ID attribute
> with value 2. The next line removes this node.
> --
> Kai Brinkmann [Microsoft]
>
> Please do not send e-mail directly to this alias. This alias is for
> newsgroup purposes only.
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
> "melanieab" <(E-Mail Removed)> wrote in message
> news:71F7E3AB-3F5E-45EE-B9E7-(E-Mail Removed)...
> > Hi,
> > I'm deleting nodes in my xml file, and it does seem to work, but then when
> > I
> > later reload the file and make an xmlNodeList, the nodelist count still
> > includes the deleted nodes yet the file shows no sign of them. Here is
> > what
> > I have:
> >
> > Example of my xml:
> > <Catalog>
> > <File ID = "1">
> > <Customer>Me</Customer>
> > <Date>Today</Date>
> > .
> > .
> > .
> > </File>
> > <File ID = "2">
> > .
> > .
> > .
> > </File>
> > and so on.
> >
> > Here's how I've deleted it:
> > string cat = @"C:\Catalog.xml";
> > XmlDocument xml = new XmlDocument();
> > xml.Load(cat);
> >
> > XmlNodeList nl = xml.GetElementsByTagName("File");
> > for (int i = 0; i < nl.Count; i++)
> > {
> > if (int.Parse(nl.Item(i).Attributes["ID"].Value) == 2)
> > {
> > nl.Item(i).RemoveAll();
> > xml.Save(cat);
> > }
> > } (I'm trying to delete File 2 as well as all it's children.)
> >
> > So later, I want to load the data in the xml file and create a new
> > XmlNodeList.
> > I was getting errors so I checked the nl.Count.ToString() and it was still
> > the original number before deletion. But upon opening the xml file, there
> > is
> > no sign of the nodes I deleted.
> >
> > Thanks again for any help!
> > Mel

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Navagation bars duplicating and deleting, not working correctly. =?Utf-8?B?Z2VlZ2Vl?= Microsoft Frontpage 1 18th Feb 2006 10:48 AM
Access report does not printing correctly but preview correctly! Ngoc Dang Dinh via AccessMonster.com Microsoft Access Reports 0 10th May 2005 02:58 AM
Access report does not printing correctly but preview correctly! Ngoc Dang Dinh via AccessMonster.com Microsoft Access ADP SQL Server 0 10th May 2005 02:57 AM
"Microsoft jet database engine could not find object MSysDB. Make sure the object exists and that you spell its name correctly and the path name correctly." -- 3011 Ricardo Microsoft Access Database Table Design 1 8th Dec 2004 07:27 PM
Outlook 2003 Not Deleting Correctly Phillip Microsoft Outlook Discussion 0 24th Jun 2004 12:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:30 AM.