XmlDocument vs XPathDocument performance

Hi...

I've been trying to improve the performance of our xml handling, so I've 
been doing some timing tests lately and ran across something I thought odd - 
XPathDocument seems slightly slower than XmlDocument.  I've run the tests 
several times and have tried to eliminate extraneous variables like disk 
access but every time, XPathDocument comes up slower by a few percent.

I'd expected it to be faster since it's a read-only representation.  Am I 
doing something wrong?

start = Timers.QueryPerformanceCounter();
for (int idx = 0; idx < 50000; idx++)
{
    StringReader sr = new StringReader(xmldoc);
    doc = new XmlDocument();
    doc.Load(sr);
}
start = Timers.SinceMilliSeconds(start);
Console.WriteLine(start);
			
start = Timers.QueryPerformanceCounter();
for (int idx = 0; idx < 50000; idx++)
{
    StringReader sr = new StringReader(xmldoc);
    pDoc = new XPathDocument(sr);
}
start = Timers.SinceMilliSeconds(start);
Console.WriteLine(start);

In other tests, XPathDocument seemed to improve 
XslCompiledTransform.Transform performance very slightly, but not enough to 
justify the switch given that the loading seemed more expensive...

Thanks
Mark

0
mmodrall1 (25)
6/5/2008 3:56:03 PM
dotnet.xml 7266 articles. 0 followers. Follow

5 Replies
961 Views

Similar Articles

[PageSpeed] 3

Hi Mark,

As for the XPathDocument, it has different implementation internally. You 
can use reflector to check its constructor. It use an Additional 
XPathDocumentBuilder and the loading process is also a bit more complex 
than the XmlDocument. I think this is probably due to the specific 
implementation required for XPathNaviagtor generation.  

For your scenario, if you haven't particular XPath based navigation 
requirement, a normal XmlDocument is enough for standard DOM based 
read/edit.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to 
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no 
rights.--------------------
>From: =?Utf-8?B?TWFyaw==?= <mmodrall@nospam.nospam>
>Subject: XmlDocument vs XPathDocument performance
>Date: Thu, 5 Jun 2008 08:56:03 -0700

>
>Hi...
>
>I've been trying to improve the performance of our xml handling, so I've 
>been doing some timing tests lately and ran across something I thought odd 
- 
>XPathDocument seems slightly slower than XmlDocument.  I've run the tests 
>several times and have tried to eliminate extraneous variables like disk 
>access but every time, XPathDocument comes up slower by a few percent.
>
>I'd expected it to be faster since it's a read-only representation.  Am I 
>doing something wrong?
>
>start = Timers.QueryPerformanceCounter();
>for (int idx = 0; idx < 50000; idx++)
>{
>    StringReader sr = new StringReader(xmldoc);
>    doc = new XmlDocument();
>    doc.Load(sr);
>}
>start = Timers.SinceMilliSeconds(start);
>Console.WriteLine(start);
>			
>start = Timers.QueryPerformanceCounter();
>for (int idx = 0; idx < 50000; idx++)
>{
>    StringReader sr = new StringReader(xmldoc);
>    pDoc = new XPathDocument(sr);
>}
>start = Timers.SinceMilliSeconds(start);
>Console.WriteLine(start);
>
>In other tests, XPathDocument seemed to improve 
>XslCompiledTransform.Transform performance very slightly, but not enough 
to 
>justify the switch given that the loading seemed more expensive...
>
>Thanks
>Mark
>
>

0
stcheng1 (64)
6/6/2008 6:43:45 AM
Hi Steven...

There are a number of cases, such as applying xsl stylesheets where we don't 
require the source to be read/write.  I'd assumed that since XPathDocument 
wouldn't have to support the write side of things that it might be faster for 
read-only type applications.  I was surprised to find that if anything it 
seemed slightly slower, and that was odd to me.

I can understand given the widespread use that a *lot* of effort went into 
optimizing XmlDocument, but I was looking to shave a few cycles going 
read-only where I could.  I was just surprised that it went the other way.

Thanks
Mark


"Steven Cheng [MSFT]" wrote:

> Hi Mark,
> 
> As for the XPathDocument, it has different implementation internally. You 
> can use reflector to check its constructor. It use an Additional 
> XPathDocumentBuilder and the loading process is also a bit more complex 
> than the XmlDocument. I think this is probably due to the specific 
> implementation required for XPathNaviagtor generation.  
> 
> For your scenario, if you haven't particular XPath based navigation 
> requirement, a normal XmlDocument is enough for standard DOM based 
> read/edit.
> 
> Sincerely,
> 
> Steven Cheng
> 
> Microsoft MSDN Online Support Lead
> 
> 
> Delighting our customers is our #1 priority. We welcome your comments and 
> suggestions about how we can improve the support we provide to you. Please 
> feel free to let my manager know what you think of the level of service 
> provided. You can send feedback directly to my manager at: 
> msdnmg@microsoft.com.
> 
> ==================================================
> Get notification to my posts through email? Please refer to 
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications.
> 
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no 
> rights.--------------------
> >From: =?Utf-8?B?TWFyaw==?= <mmodrall@nospam.nospam>
> >Subject: XmlDocument vs XPathDocument performance
> >Date: Thu, 5 Jun 2008 08:56:03 -0700
> 
> >
> >Hi...
> >
> >I've been trying to improve the performance of our xml handling, so I've 
> >been doing some timing tests lately and ran across something I thought odd 
> - 
> >XPathDocument seems slightly slower than XmlDocument.  I've run the tests 
> >several times and have tried to eliminate extraneous variables like disk 
> >access but every time, XPathDocument comes up slower by a few percent.
> >
> >I'd expected it to be faster since it's a read-only representation.  Am I 
> >doing something wrong?
> >
> >start = Timers.QueryPerformanceCounter();
> >for (int idx = 0; idx < 50000; idx++)
> >{
> >    StringReader sr = new StringReader(xmldoc);
> >    doc = new XmlDocument();
> >    doc.Load(sr);
> >}
> >start = Timers.SinceMilliSeconds(start);
> >Console.WriteLine(start);
> >			
> >start = Timers.QueryPerformanceCounter();
> >for (int idx = 0; idx < 50000; idx++)
> >{
> >    StringReader sr = new StringReader(xmldoc);
> >    pDoc = new XPathDocument(sr);
> >}
> >start = Timers.SinceMilliSeconds(start);
> >Console.WriteLine(start);
> >
> >In other tests, XPathDocument seemed to improve 
> >XslCompiledTransform.Transform performance very slightly, but not enough 
> to 
> >justify the switch given that the loading seemed more expensive...
> >
> >Thanks
> >Mark
> >
> >
> 
> 
0
mmodrall1 (25)
6/6/2008 1:00:02 PM
"Mark" <mmodrall@nospam.nospam> wrote in message
news:DB816F0B-1CF1-43C3-A337-4187F40EA692@microsoft.com...
> Hi Steven...
>
> There are a number of cases, such as applying xsl stylesheets where we
don't
> require the source to be read/write.  I'd assumed that since XPathDocument
> wouldn't have to support the write side of things that it might be faster
for
> read-only type applications.  I was surprised to find that if anything it
> seemed slightly slower, and that was odd to me.
>
> I can understand given the widespread use that a *lot* of effort went into
> optimizing XmlDocument, but I was looking to shave a few cycles going
> read-only where I could.  I was just surprised that it went the other way.
>

I'm not sure why there not being code present to perform write operations
would have any impact on read performance.

Have you tried examining the memory requirements between the two of them?

I haven't done that myself but I suspect XPathDocument's true strength lies
in its application in a multi-threaded environment such as ASP.NET where
many such documents are being loaded and read.  This where memory usage is
likely to have a greater impact on performance than sheer CPU grunt.



-- 
Anthony Jones - MVP ASP/ASP.NET


0
Ant1508 (14)
6/7/2008 2:02:08 PM
"Anthony Jones" wrote:
> I'm not sure why there not being code present to perform write operations
> would have any impact on read performance.
> 
> Have you tried examining the memory requirements between the two of them?
> 
> I haven't done that myself but I suspect XPathDocument's true strength lies
> in its application in a multi-threaded environment such as ASP.NET where
> many such documents are being loaded and read.  This where memory usage is
> likely to have a greater impact on performance than sheer CPU grunt.

I haven't profiled the memory requirements either, but the theory (perhaps 
half-baked) was that a read-only implementation would be able to jettison a 
lot of code paths and have fewer checks in the existing methods that go to 
support write operations.

I know it's not quite apples to apples, but I thought XPathDocument might be 
half way to a sax implementation in terms of being lighter weight and faster.

My first tests included doing a transform on the same ~3k xml document.  
Then I separated the times for loading and transforming.  XPathDocument was 
very slightly faster on transforms but not enough to balance the extra 
slowness of getting a loaded instantiation of it.

As I said, that came as a bit of a surprise to me.

Mark
0
mmodrall1 (25)
6/7/2008 5:31:00 PM
Hi Mark,

Yes, theoretically XPathDocument only perform read access, however, I think 
the Xpath specific implementation here add the most overhead.  So far for 
the xml API in .net framework, the xml reader/writer is the most light 
weight and effecient one(a customized SAX model).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to 
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no 
rights.--------------------
>Subject: Re: XmlDocument vs XPathDocument performance
>Date: Sat, 7 Jun 2008 10:31:00 -0700

>
>
>"Anthony Jones" wrote:
>> I'm not sure why there not being code present to perform write operations
>> would have any impact on read performance.
>> 
>> Have you tried examining the memory requirements between the two of them?
>> 
>> I haven't done that myself but I suspect XPathDocument's true strength 
lies
>> in its application in a multi-threaded environment such as ASP.NET where
>> many such documents are being loaded and read.  This where memory usage 
is
>> likely to have a greater impact on performance than sheer CPU grunt.
>
>I haven't profiled the memory requirements either, but the theory (perhaps 
>half-baked) was that a read-only implementation would be able to jettison 
a 
>lot of code paths and have fewer checks in the existing methods that go to 
>support write operations.
>
>I know it's not quite apples to apples, but I thought XPathDocument might 
be 
>half way to a sax implementation in terms of being lighter weight and 
faster.
>
>My first tests included doing a transform on the same ~3k xml document.  
>Then I separated the times for loading and transforming.  XPathDocument 
was 
>very slightly faster on transforms but not enough to balance the extra 
>slowness of getting a loaded instantiation of it.
>
>As I said, that came as a bit of a surprise to me.
>
>Mark
>

0
stcheng1 (64)
6/9/2008 2:07:12 AM
Reply:

Similar Artilces:

outlook vs. live mail conflict
I have outlook on desktop, with XP, and live mail on new laptop, with Vista. Recently the laptop started automatically moving new msgs to deleted folder from inbox although they haven't yet been read, if they have been opened first on the desktop with outlook. Any clues why? Is the account on the Outlook PC setup to leave a copy of the message on the server? "charlieB" <charlieB@discussions.microsoft.com> wrote in message news:4578A9BF-7904-4159-823E-D175548236ED@microsoft.com... > I have outlook on desktop, with XP, and live mail on new laptop, with ...

XMLWriter and XMLDocument
Okay I am using an XMLDocument object to process several thousands of lodgements (from a database) I am trying to output it from an XML Document to a file using the XmlDocument.WriteTo() method with an XmlWriter. It writes 131kB and then cuts out halfway through an element Is this a limitation of XmlDocument.WriteTo() ? What would be a better way to output it Thanks Ensure you're calling XmlWriter.Close() to cause the buffered writes to be flushed to the underlying stream/textwriter. -- Daniel Cazzulino [MVP XML] Clarius Consulting SA http://weblogs.asp.net/cazzu http://aspnet2.com ...

GP Standard vs Professional
Hi, Can anybody point me to a link or just give a brief description of differences between Standard and Professional versions of Great Plains. Some of the modules we are interested in work only on Pro version. Milan. Hi Milan You can find information at: https://mbs.microsoft.com/partnersource/products/greatplains/sales/salestools/productfactsheets/gp_smb_comparison_guide.htm?printpage=false Regards, Takashi "Milan" <milan@online.microsoft.com> wrote in message news:%23h$M0omJFHA.616@TK2MSFTNGP10.phx.gbl... > Hi, > > Can anybody point me to a link or just giv...

MFC Dlls between VS 6, and VS 7.1
Hi there, quick question about MFC dlls. If I compile a simple "hello world" app in Visual C++ 6, and I type in at the command prompt: dumpbin /dependents hello.exe I get a list of the DLLs that hello.exe depends on: MFC42.DLL MSVCRT.dll KERNEL32.dll USER32.dll GDI32.dll COMCTL32.dll OLEAUT32.dll I do not have visual studio.net installed on my machine (yet). Could someone compile a simple MFC hello world app using VS 7.1 and see what DLLs it depends on using dumpbin? I'm asking because I know my clients only have MFC42.dll on their machines and I...

PM EFT vs. European EFT in GP10
Hello all, In previous versions of GP, when we have set up customers for EFT, we have done the following: - Enable "DDR and European Electronic Funds Transfer" in Company Options; - Set a file output path in Posting Options for the EFT Payment Register; - Used Report Writer to modify the PM EFT Payment Register to make it look like a BACS file. All of which worked a treat, despite feeling like a bit of a workaround. With the release of GP 10, it's not gone unnoticed that the core EFT features have been beefed up somewhat, allowing the file format to be created through the UI,...

32bit vs 64bit IE8
I am running Vista 64bit Home. Why is my IE8 not 64bit also? What would be the advantages to updating to the 64bit version? -- Loren Brothers Orion Creative Works You will not be bothered by all those Flash animations that fill up your screen and your bandwidth. Flash is, still, not available in 64-bit. "Loren_Brothers" <loren_brothersAThotmailDOTcom> wrote in message news:3344F8AA-E649-491E-9D83-343ADD573EA2@microsoft.com... :I am running Vista 64bit Home. Why is my IE8 not 64bit also? What would be : the advantages to updating to the 64bit version? : -...

Performance issue on writing timephased data
Someone knows why writing timephased data through code has such a bad performance? I've have develop a procedure to import the assignment timephased data from a excel file into project. This procedure is a Project add-in written in c#. Here's a sample of the code that runs for each assignment: Code: -------------------- oRCProject.Application.OptionsCalculation(false, false, false, false, false, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); for (int...

VS Tools
I would like to be able to click Yes/No etc on a dialog box or enter information into a dialog box that pops up as a result of me clicking a button using VS tools Dynamics GP. I know how to respond to dialog boxes in VBA, so I am guessing that there must be a way to respond to them using VS Tools. Has anyone done this? Curtis, Unfortunately your guess is incorrect - neither VSTools or Dexterity can answer modal dialog boxes at this time. As you note, VBA can. This would make a good product suggestion and I'd encourage you to enter it from partnersource/customersource. As 10.0...

ReadyBoost monitoring via Performance Monitor, Counter Missing?
I've seen many tutorials on monitoring ready boost in Vista to see if its performing correctly, however I seem to be missing the counters in Performance Monitor to monitor it? How do I add them, are they Windows 7 only? Seems they aren't installed on my system? I tried the tutorial at http://www.bleepingcomputer.com/tutorials/tutorial136.html and this is how I noticed I am missing these counters. I also tried a program created to monitor it, but it errors out (probably because it uses the same performance counters I am missing?) Found it at: http://area-71.net/rbmon...

Excel Range Value issue (Excel 97 Vs Excel 2003)
Hi, I have some VB6 code which was originally written to populate a spreadsheet in EXCEL 97. Basically, to cut a long story short, what the code does is it populates a variant array by calling a stored procedure. The variant array in question is populated with 3 elements (a date, and 2 floats). The array is then passed to Excel like this. Dim xlRng As Excel.Range xlRng.Value = vntDataArray In my particular scenario, the first four rows have the following dates in the "dd/mm/yyyy" format. 29/11/1995 30/11/1995 01/12/1995 04/12/1995 These dates are exported perfectly to Excel 97...

XmlDocument with URL
My goal is to load an xml document from a remote url, make some changes to it, and then display it in a webpage. Say my codebehind looks like this private void Page_Load(Object sender, EventArgs e) { XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8); XmlDocument doc = new XmlDocument(); doc.Load(http://somexmlgeneratedhere.com?id=333); //manipulate the doc here doc.WriteTo(writer); } Im not getting the full document from the URL, I'm sure Im doing this wrong. Can anyone point me in the right direction? thanks in advance Dave He...

Image/Frame manipulation vs. QuarkXpress
I have a try version of Publisher 2003. QuarkXpress allows the user to adjust, scale or reposition a downloaded image within a fixed frame (the frame and image can be adjusted independently). It appears that Publisher does not have a similar ability. Is this observation correct, or have I just overlooked how to manipulate images? Thanks --DB Everybody does stuff differently<G> Have you tried right clicking on an image and selecting "format picture" from the flyout? investigate the possiblities in there. (esp the "crop" function). For interactive, use the pi...

Psexec vs MFC application
I am having problems starting an MFC application in remote using psexec (from http://www.sysinternals.com/). It works well with most applications (ex: notepad.exe), but it creates problems with MFC applications. The application starts, but it aborts with an "Application Error" saying "Windows cannot continue from this exception". We then wrote our own psexec-like tool to spawn remote applications, but we have the same problem. The way psexec (and our tool) works, is by starting a service on the remote computer, which then waits for a request to start a process locally. ...

XSLT transform XmlDocument to XmlDocument?
I just want to make a transformation on the fly, without having to save the XML document to a file before or after the transformation. I don't find any examples of this. I see that the XslTranform.Transform() method can return an XmlReader, but how can that reader be converted to an XmlDocument object? Big thanks, Gustaf Gustaf Liljegren wrote: > I just want to make a transformation on the fly, without having to save > the XML document to a file before or after the transformation. I don't > find any examples of this. See <http://groups-beta.google.com/group/mic...

excel vs quattro
Is there a feature in excel that is the same as quattro's feature "Notebook query"? ...

Internal problem on XmlDocument.Load( )
I have this really frustrating problem when calling XmlDocument.Load(string filename). Whenever I call this method in a web application in my Windows XP development PC, I get the following error: Specified argument was out of the range of valid values. Parameter name: The specified value must be greater than 0. Here's the stack trace: ------------------------- [ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: The specified value must be greater than 0.] System.Net.ServicePoint.set_ConnectionLimit(Int32 value) +60 System.Xml.XmlDo...

outlook vs outlook express #7
Is it possible to import email messages from Outlook Express into Outlook. I accidentally opened up my email using POP and Outlook Express thereby loosing the messages in my Outlook inbox. How do I download or transfer back the lost messasges in Outlook from Outlook express. All my official emails are now on Express. Help please. ...

Outlook 2007 vs 2003
I have just upgraded my home version of Office. I run 2007 on Vista. At the office where I use a laptop, we have Office 2003 and XP. I use Outlook on both configured to the same e-mail address. I have two questions: 1. I have marked the option on both systems to leave messages on the server so that I can read new messages from either location. While messages I pick up at the office appear in my home inbox, messages that I download to my home computer do not appear in my office outlook. Is there something else I can do? 2. I have also tried to import my home address book into my offi...

Administrator vs User
When I installed Windows on this new hard drive, I never set up a user account - just administator - and used no passwords. That is the way it is on other two hard drives. For 3 days, until I updated Windows, I never had to log in. Now after updates I have only one choice to log in - that is as "Ron Patterson" In Control Panel / User Accounts there are three icons: Ron Patterson, Computer Administrator Guest - "Guest account is off" Asp Net Machine - Limited Account When I boot up I have to log in as user - Ron Patterson. But when I try to...

Remove exchange attributes vs. Delete Mailbox
Hi, What negative effects can appear if "Remove exchange attributes" is used to delete mailboxes within an Exchange 2003 environment instead of the "delete mailbox" task ? Unfortunately at http://msexchangeteam.com/archive/2006/10/13/429192.aspx and http://support.microsoft.com/kb/823170 no side effects are documented.... Thanks Mario ...

XPathDocument to xmldocument
How to create XPathDocument object from xmldocument object thanks vikram wrote: > How to create XPathDocument object from xmldocument object First, be aware that such converting means copying the whole set of data in the XML document. Second, it's easy: XPathDocument doc = new XPathDocument(new XmlNodeReader(xmlDoc)); -- Oleg Tkachenko [XML MVP, MCP] http://blog.tkachenko.com ...

Loading XmlDocument with a file on network?
Can you load an XmlDocument with a file located on a network share? I thought maybe the XmlResolver could be used XmlUrlResolver resolver = new XmlUrlResolver() resolver.Credentials = CredentialCache.DefaultCredentials // Create and load the XmlDocument XmlDocument doc = new XmlDocument() doc.XmlResolver = resolver; // Set the resolver doc.Load(@"\\devsrv\profiles\0004.xml") OR.. doc.Load("//devsrv/profiles/0004.xml") But I get: "Logon failure: unknown user name or bad password" The problem is probably that the account that your application is running under...

which better: Frequency vs Histogram
I read "Explore Histograms" in http://office.microsoft.com/en-us/assistance/HA011109481033.aspx and found it educational. However, I can't see a difference in output between using a Histogram and using the Frequency function. Can anybody share the pros and cons of each? I think you have missed the point. The article says that the table produced by the ATP histogram tool is exactly the same as the table (with same bin definitions) produced by the FREQUENCY function. The difference is not in the values calculated, but in the fact that the histogram table is a static t...

XP styles and Unicode in VS 2005
I need to use XP style in my MFC projects. My projects are not UNICODE. In VS 2003 i was able to use XP styles. I have imported the same projects in VS 2005 and I am not able to use XP styles. Is it possible to use XP styles in NOT UNICODE projects in VS 2005? "BobbyJones" <BobbyJones@discussions.microsoft.com> wrote in message news:3B263D28-F094-4C1F-BAB5-A35DDD86FAF3@microsoft.com... >I need to use XP style in my MFC projects. > My projects are not UNICODE. > In VS 2003 i was able to use XP styles. > I have imported the same projects in VS 2005 and I am not...

Leads and B2C vs. B2B
I'm still trying to understand the basic flow of states between leads, contacts, accounts and opportunities. We are in a direct relations with our customers and _we_ call potential clients. So leads make no sense for us, so I guess we have to start directly with accounts. Is that correct? I am also correct to assume that a client should be a contact in a B2C relation and an account in a B2B relation? Lastly, if we are in a B2C relation, we can't really use leads, which require a Company field, can we? Or am I completely misunderstanding the logic of the application? Leads a...