Clone an XmlDocument and XmlResolver

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
 
I am using System.Xml to load and manipulate XHTML document. I have noticed that if I have a DOCTYPE directive in my xhtml file, my performance are very bad. I have done researches and I found that problem come from XmlResolver use to resolve entity reference. So I have made my own XmlResolver, which manage a local file cache.
For exemple
<font color="#2b91af  
<font size=2></font>
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% <font size=2>Code Snippet</font> <font color="#2b91af
<font size=2>XmlDocument</font></font><font size=2> doc = <font color="#0000ff new</font> <font color="#2b91af XmlDocument</font>();</font>
<font size=2>doc.XmlResolver = <font color="#0000ff new</font> <font color="#2b91af XmlCachedFileResolver</font>();</font>
<font size=2>doc.Load(<font color="#2b91af File</font>.OpenRead(<font color="#a31515 "MasterTemplate.htm"</font>));</font>
<font size=2> </font> </font>

 
<font size=2><font size=3>works fine and faster than</font>
 

<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
XmlDocument<font size=2> doc = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlDocument</font><font size=2>(); </font><font size=2>
doc.Load(</font><font color="#2b91af" size=2>File</font><font size=2>.OpenRead(</font><font color="#a31515" size=2>"MasterTemplate.htm"</font><font size=2>));</font>
 
<font size=2></font> 
<font size=2><font size=3>But my problem is I want to use the Clone method on my document. But Clone construct a new XmlDocument, with the default resolver, it takes long long time and I didnt find a way to use my own resolver.</font></font>
<font size=2><font size=3>For exemple</font></font> <font size=2><font color="#2b91af" size=2>

<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet <font size=2><font color="#2b91af" size=2>
XmlDocument</font><font size=2> doc = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlDocument</font><font size=2>();
doc.XmlResolver = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlCachedFileResolver</font><font size=2>();
doc.Load(</font><font color="#2b91af" size=2>File</font><font size=2>.OpenRead(</font><font color="#a31515" size=2>"MasterTemplate.htm"</font><font size=2>));</font> <font color="#2b91af" size=2>
XmlDocument</font><font size=2> res = (</font><font color="#2b91af" size=2>XmlDocument</font><font size=2>) doc.Clone(); </font><font color="#008000" size=2><font color="#008000" size=2></font></font></font>
 
<font color="#000000" size=3>is slower than</font>
<font color="#000000" size=3></font>  <font color="#000000" size=3><font color="#2b91af" size=2>

<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet <font color="#000000" size=3><font color="#2b91af" size=2>
XmlDocument</font><font size=2> doc = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlDocument</font><font size=2>();
doc.XmlResolver = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlCachedFileResolver</font><font size=2>();
doc.Load(</font><font color="#2b91af" size=2>File</font><font size=2>.OpenRead(</font><font color="#a31515" size=2>"MasterTemplate.htm"</font><font size=2>)); </font></font><font color="#000000" size=3><font color="#2b91af" size=2>
XmlDocument</font><font size=2> res = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlDocument</font><font size=2>();
res.XmlResolver = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlCachedFileResolver</font><font size=2>();
res.InnerXml = doc.InnerXml; </font></font>
 </font></font>
<font color="#000000" size=3></font> 
<font color="#000000" size=3>My test result</font>
<font color="#000000" size=3>Without DOCTYPE directive               Load : 0.6 ms     Clone : 0.06 ms</font>
<font color="#000000" size=3>With DOCTYPE                                    Load : 2075 ms  Clone : 1884 ms</font>
<font color="#000000" size=3>With DOCTYPE/custom resolver        Load : 87 ms      Clone : 1970 ms</font>
<font color="#000000" size=3>With DOCTYPE/last solution               Load : 83 ms      InnerXml : 31 ms</font>
 
<font color="#000000" size=3>There is a way to set a resolver for Clone method (or other solution to improve performance) ?</font>
<font color="#000000" size=3></font> 
<font color="#000000" size=3>Thanks</font>
<font color="#000000" size=3></font> 
<font color="#000000" size=3></font>  </font>
<font color="#008000" size=2><font color="#008000" size=2>
</font></font></font></font>

View the full article
 
Back
Top