昨天在这儿看到一个链结:msdn上的一篇关于名为Efficient Techniques for Modifying Large XML Files的文章。修改XML文件对我来说,一直是件头痛的事件,当XML文件很大时,用XmlDocument处理效率就很低了,但这篇文章也没有给出一个满意的答案,全文讲了两个例子:感觉第一个例子还有点趣:使用XML 的 inclusion概念来使的文章便于扩展。他创建了两个文档,第一个是well format的xml文档,第二个是xml引用文档,内容如下:
Logfile.xml:
<?xml version="1.0"?>
<!DOCTYPE logfile [
<!ENTITY events
SYSTEM "logfile-entries.txt">
]>
<logfile>
&events;
</logfile>
logfile-entries.txt:
<event>
<ip>127.0.0.1</ip>
<http_method>GET</http_method>
<file>index.html</file>
<date>2004-04-01T17:35:20.0656808-08:00</date>
</event>
<event>
<ip>127.0.0.1</ip>
<http_method>GET</http_method>
<file>logo.gif</file>
<date>2004-04-01T17:35:25.238220-08:00</date>
<referrer>http://www.example.com/index.html</referrer>
</event>
一看,便知其意。这种方法处理Append确实很棒。但其它形式的修改就无能为力了。
文章的第二部分,如何使用XmlTextReader和XmlTextWriter来快速检索和写数据,他的意思还是想避开使用XmlDocument.在这个例子中,他使用到了XmlNameTable,意思是地址比较比串比较速度快。
如下:XmlNameTable xnt = new NameTable(); //XmlNameTable是抽象类,NameTable是它的一个具体实现。
string ip = xnt.Add("ip");
XmlTextReader xr = new XmlTextReader(xnt);//使用XmlNameTable初始化XmlTextTable
xr.Read();
if(xr.Name == ip) //估计作者的意思是这儿比较会加快速度。
{}
然后作者在文章未尾,又给了连接:Object Comparison Using XmlNameTable with XmlReader
有如下内容:The XmlNameTable class enables the implementations of the XmlReader class to use pointer comparisons instead of string comparisons when parsing data or performing comparison operations on XML documents. The use of this table gives performance gains to the XmlReader derived classes when comparing and using element and attribute names.
估计就是作者想表达的意思。