473,395 Members | 1,762 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,395 software developers and data experts.

RemoveNode

What is the fastest way to do the following, using System.xml v2.0?

System.Xml.XmlDocument document = new System.Xml.XmlDocument();
document.Load(@"test.xml");
System.Xml.XmlNodeList Nodes = document.SelectNodes("xml/foo/bar");
foreach (System.Xml.XmlNode Node in Nodes) {
Node.ParentNode.RemoveChild(Node);
}

The RemoveChild method is not performant in 1.0. I am trying to find out
what is the absolute fastest way to remove a set of nodes (i.e.
System.Xml.XmlNodeList) in v2 of System.xml

Thanks,
Scott
Nov 12 '05 #1
1 2233
"Scott" <Sc***@discussions.microsoft.com> wrote in message news:AF**********************************@microsof t.com...
The RemoveChild method is not performant in 1.0. I am trying to find out
what is the absolute fastest way to remove a set of nodes (i.e.
System.Xml.XmlNodeList) in v2 of System.xml


It's not using System.Xml.XmlNodeList, if you want "absolute" speed
then you need to use an XmlReader, like this one (which removes all
<bar> tags and their descendants):

- - - RemoveTagXmlReader.cs
using System;
using System.Xml;

internal class RemoveTagXmlReader : XmlTextReader
{
private object atomRemoveThis;

public RemoveTagXmlReader( string tagNameToRemove, string filename)
: base( filename )
{
this.atomRemoveThis = this.NameTable.Add( tagNameToRemove);
}

public override bool Read( )
{
bool result = base.Read( );
if (this.NodeType == XmlNodeType.Element &&
this.atomRemoveThis == (object)this.LocalName)
{
this.Skip( );
return this.Read( );
}
return result;
}
}

public class TestApp
{
static void Main( string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings( );

// If you want to produce an output file preserving this information,
// then set these to false.
//
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreWhitespace = true;
// . . .
// Try turning off CheckCharacters and ConformanceLevel if you
// don't need them for extra boost.

using( XmlReader reader = XmlReader.Create( new RemoveTagXmlReader( "bar", "test.xml"), settings))
{
while (reader.Read( ))
{
Console.WriteLine( string.Format( "{0} {1} {2} {3}",
"\t".PadLeft(reader.Depth), reader.NodeType,
reader.LocalName, ((reader.Value == null) ? "(null)" : reader.Value )));
}
}
}
}
- - -

Your key takeaways here are:

1. Not using XmlNodeList. (If you need the luxury of XmlNodeList, learn
to like your XML slow.)

2. When I create the XmlTextReader subclass, I "atomize" the string I'm
going to be doing comparisons against by adding it to the XmlNameTable.

This permits faster object reference identity comparisons to be performed
on the string when it's a element, prefix, namespace URI or attribute name,
rather than char-by-char comparisons (ie, you get the MSIL ceq opcode,
not a call to the String::op_Equality( ) function which is more expensive).

3. You could argue that using the XmlTextReader subclass directly may
be more efficient than using .NET 2.0's Factory Pattern to instantiate
an XmlReader because the latter puts my custom derived XmlTextReader
into a wrapper.

It's a trade-off you need to evaluate based on what your intended use
of the XmlReader is going to be. You'll gain performance by turning
off the unnecessary features. In the example above, to get at the heart
of the Infoset, comments, PIs, and insig. whitespace were discarded.

In this case where only five instance members of the XmlReader are
getting accessed, not making the XmlReader process extraneous
content saves me much more time then the cost of the one additional
level of indirection introduced by the wrapper Create( ) puts around
me.

When you're reading "pretty-printed" XML in general, freeing your-
self from the processing of extra whitespace nodes between content
is usually enough to justify accepting the wrapper (e.g., prefer the
wrapper Create( ) gives you over using your XmlTextReader sub-
class independently).

4. The Read( ) override makes use of the Skip( ) method to bypass
the node you wish to remove and all it's descendants. Any application
code that consumes this XmlTextReader subclass will never know that
those nodes existed.
You never said what you intended to do with the XML after you
had removed it's <bar> nodes. That'll dictate where you go from
here. If you really wanted to get this XML into an XmlDocument
or XPathDocument, then you can do something like this (instead
of my while loop),

XmlDocument doc = new XmlDocument( );
doc.Load( reader);

or,

XPathDocument xpathDoc = new XPathDocument( reader);

Derek Harmon
Nov 12 '05 #2

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

Similar topics

2
by: nifsmith | last post by:
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded...
12
by: Ole Noerskov | last post by:
The function below is supposed to remove all childnodes with a name that starts with "keywords" in "myform" in the window.opener. It works fine if there's one keyword node. But you have to run...
3
by: Tim Mackey | last post by:
hi, i have a javascript function to highlight google search keywords in the page. it works well on IE and mozilla browsers. for the page OnLoad, i call the Highlight() method, and that...
1
by: Jonatan | last post by:
Hi all, I'm trying to upload files doing something like gmail. I almost got it but I have a problem when I post the form. Basicaly, I am doing the following. When the user presses the link...
5
by: steve.macleod | last post by:
Hi, I am using removeNode() to remove a table (tbody) from the document like this: the_table = document.getElementById(the_table_id); the_table.removeNode(true); However when I access the...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
3
by: asianmuscle | last post by:
Hi I am relearning datastructure... but got kinda stuck in a basic delete node operation. what it does is to delete the first node it finds when the item is equal the input item. the end result is...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.