473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Remove and insert

Hi!

I wanted to select a subset of nodes (list =
selectNodes("parent/child[foo='bar']") from a XmlDocument, then remove all
(parentNode.removeAll();) child-nodes and insert the previous selected nodes
back. Of course the XmlNodes in the nodelist ref. where lost once the
removeAll() were executed, this is expected.

So the obvious way to achive the result would be to clone the nodes or
remove the nodes not qualifying the Xpath.
Well I belive I have found an, for me, unexpected shortcut. If I refer the
list.count property prior to removeAll(), the nodes is still referable and
can be inserted back.

The explanation for this is, as understad it, is that XmlNodeList.Count is a
heap based data type, Integer, and therefore addRef+1 to all selected nodes
when .Count is executed, and therefore they are not marked as collectable.

I've tried searching comments on this one but found nothing. Well, maybe
it's just me that belive this particular behaviour of XmlDocument is
remarkable?

Please comment or correct me, regards,
Markus

P.o.c. (Winform/C#)

XmlDocument doc = new XmlDocument();
doc.LoadXml("<parent><child><foo>bar</foo></child><child><foo>bar</foo></child><child><foo>not bar</foo></child></parent>");

XmlNodeList list = doc.SelectNodes("parent/child[foo='bar']");

MessageBox.Show("Nr. of childs before removeAll()= "+
doc.SelectNodes("parent/child").Count.ToString());

// Remove the comments on the line below to keep nodes in nodelist referable
//int c = list.Count;

doc.SelectSingleNode("parent").RemoveAll();

MessageBox.Show("Nr. of childs after removeAll()= "+
doc.SelectNodes("parent/child").Count.ToString());

foreach(XmlNode item in list)
doc.SelectSingleNode("parent").AppendChild(item);

MessageBox.Show("Nr. of childs after appending list= "+
doc.SelectNodes("parent/child").Count.ToString());


Dec 2 '05 #1
3 2633


Markus wrote:

I wanted to select a subset of nodes (list =
selectNodes("parent/child[foo='bar']") from a XmlDocument, then remove all
(parentNode.removeAll();) child-nodes and insert the previous selected nodes
back. Of course the XmlNodes in the nodelist ref. where lost once the
removeAll() were executed, this is expected.


XmlDocument doc = new XmlDocument();

doc.LoadXml("<parent><child><foo>bar</foo></child><child><foo>bar</foo></child><child><foo>not
bar</foo></child></parent>");
XmlNodeList list = doc.SelectNodes("parent/child[foo='bar']");
Console.WriteLine("list.Count: {0}.", list.Count);
doc.DocumentElement.RemoveAll();
Console.WriteLine("list.Count: {0}.", list.Count);

gives

list.Count: 2.
list.Count: 2.

for me so why is something lost? .NET has its own conventions about
XmlNodeList instances, those returned by SelectNodes are not live
collections so they don't change when the document changes. It is
different in .NET with XmlNodeList instances returned by
GetElementsByTagName, those are live collections that are updated when
the document changes so this example:

XmlDocument doc = new XmlDocument();

doc.LoadXml("<parent><child><foo>bar</foo></child><child><foo>bar</foo></child><child><foo>not
bar</foo></child></parent>");
XmlNodeList list = doc.GetElementsByTagName("child");
Console.WriteLine("list.Count: {0}.", list.Count);
doc.DocumentElement.RemoveAll();
Console.WriteLine("list.Count: {0}.", list.Count);

gives

list.Count: 3.
list.Count: 0.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Dec 2 '05 #2
Ok, in your code nothings lost. But it will be IF you comment the first line
of list.count: Console.WriteLine("list.Count: {0}.", list.Count);

Snippet:
XmlDocument doc = new XmlDocument();

doc.LoadXml("<parent><child><foo>bar</foo></child><child><foo>bar</foo></child><child><foo>not bar</foo></child></parent>");
XmlNodeList list = doc.SelectNodes("parent/child[foo='bar']");
Console.WriteLine("Before remove, not doing list.Count!");
//Console.WriteLine("Before remove: {0}, doing list.Count!", list.Count);
doc.DocumentElement.RemoveAll();

Output without executing first list.count:
Before remove, not doing list.Count!
list.Count: 0.

Output executing first "list.count":
Before remove: 2, doing list.Count!
list.Count: 2.

What i try to say is that if you do not execute the first list.count, your
nodeLIst will be lost.

Best regards and have a nice weekend!
Markus

"Martin Honnen" wrote:


Markus wrote:

I wanted to select a subset of nodes (list =
selectNodes("parent/child[foo='bar']") from a XmlDocument, then remove all
(parentNode.removeAll();) child-nodes and insert the previous selected nodes
back. Of course the XmlNodes in the nodelist ref. where lost once the
removeAll() were executed, this is expected.


XmlDocument doc = new XmlDocument();

doc.LoadXml("<parent><child><foo>bar</foo></child><child><foo>bar</foo></child><child><foo>not
bar</foo></child></parent>");
XmlNodeList list = doc.SelectNodes("parent/child[foo='bar']");
Console.WriteLine("list.Count: {0}.", list.Count);
doc.DocumentElement.RemoveAll();
Console.WriteLine("list.Count: {0}.", list.Count);

gives

list.Count: 2.
list.Count: 2.

for me so why is something lost? .NET has its own conventions about
XmlNodeList instances, those returned by SelectNodes are not live
collections so they don't change when the document changes. It is
different in .NET with XmlNodeList instances returned by
GetElementsByTagName, those are live collections that are updated when
the document changes so this example:

XmlDocument doc = new XmlDocument();

doc.LoadXml("<parent><child><foo>bar</foo></child><child><foo>bar</foo></child><child><foo>not
bar</foo></child></parent>");
XmlNodeList list = doc.GetElementsByTagName("child");
Console.WriteLine("list.Count: {0}.", list.Count);
doc.DocumentElement.RemoveAll();
Console.WriteLine("list.Count: {0}.", list.Count);

gives

list.Count: 3.
list.Count: 0.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Dec 2 '05 #3


Markus wrote:

What i try to say is that if you do not execute the first list.count, your
nodeLIst will be lost.


Right, as far as I currently understand what we observe is that there
seems to be some lazy evaluation happening, meaning the XmlNodeList
instance returned by SelectNodes is only built once its content is
accessed (as our examples do with outputting the Count property).
So what nodes you find in the XmlNodeList does not depend on the XML
document or node contents when SelectNodes call happens and returns the
XmlNodeList instance but when the Count is accessed first.

I am currently asking elsewhere whether that is a bug or an intended
feature. It looks more like a bug in my understanding, if it is a
feature then it needs proper documentation.

Current test case is

XmlDocument doc = new XmlDocument();

doc.LoadXml("<parent><child><foo>bar</foo></child><child><foo>bar</foo></child><child><foo>not
bar</foo></child></parent>");
XmlNodeList list1 = doc.SelectNodes("parent/child[foo='bar']");
XmlNodeList list2 = doc.SelectNodes("parent/child[foo='bar']");
XmlNodeList list3 = doc.SelectNodes("parent/child[foo='bar']");
Console.WriteLine("Before removeAll:");
Console.WriteLine("list1.Count: {0}.", list1.Count);
doc.DocumentElement.RemoveAll();
Console.WriteLine("After removeAll:");
Console.WriteLine("list1.Count: {0}.", list1.Count);
Console.WriteLine("list2.Count: {0}.", list2.Count);
doc.DocumentElement.InnerXml =
"<child><foo>bar</foo></child><child>Kibo</child>";
Console.WriteLine("After insertion:");
Console.WriteLine("list1.Count: {0}.", list1.Count);
Console.WriteLine("list2.Count: {0}.", list2.Count);
Console.WriteLine("list3.Count: {0}.", list3.Count);

That results then in the output

Before removeAll:
list1.Count: 2.
After removeAll:
list1.Count: 2.
list2.Count: 0.
After insertion:
list1.Count: 2.
list2.Count: 0.
list3.Count: 1.

Thus although all SelectNodes call are identical and are made before the
document is being modified those three lists end up with different Count
property values depending on when Count is being first accessed.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Dec 4 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: JT | last post by:
i have written some asp that reads a fixed length text file, line by line, inserting each line into my database. my problem is that the text file format seems to have extra space at the end of the...
3
by: Diego Rey | last post by:
Hi everyone. How can I get the unique row from a table which contains multiple rows that have exactly the same values. example: create table test ( c1 as smallint, c2 as smallint, c3 as...
12
by: laurenq uantrell | last post by:
Is there an easy way to loop through all rows and remove all international alphabet characters from a column in a table, for example remove German umlauts "ü" and convert them to a simple "u"....
6
by: Arne Claus | last post by:
Hi If've just read, that remove() on a list does not actually remove the elements, but places them at the end of the list (according to TC++STL by Josuttis). It also says, that remove returns a...
7
by: sanjana | last post by:
hi i wanna detect if a anything is connected to the usb port I am using system.management class for tht purpose this is my code class usbdetect { public static void Main() {
1
by: Glenn | last post by:
Hi, I have a config XML file that I am using from the application cache. I have configured the entry with a remove callback to re-populate cache automatically when the XML file changes. All is...
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
3
by: Yas | last post by:
Hi, I am creating creating a table with a Date column dd-mm-yyyy. But I cant seem to find a SQL function that just returns today's date. getDate() returns the time as well so I cant use it. ...
1
by: pipetawer | last post by:
Greetings, I am having some kind of trouble with some code. I have the following object using System; using System.Collections; namespace ImageServerCleanup { public class DiffList :...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.