惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

博客园 - 什么都不知道

DataSet的Xml序列化问题 VB.NET的一个小问题 Access is denied的问题 - 什么都不知道 存储过程output参数问题 SQL Server的效率? datagrid刷新问题 使用Visio DrawingControl的应用开发(补) 突起效果的Label WinForm下TextBox的数据绑定和更新 使用Radio按钮选择DataGrid行 如何在运行时加载不处于应用程序目录下的assembly 使用VSA给程序加上脚本支持 删除所有Windows组件 在ASP.NET中嵌入wml标记 c#中动态装载dll RedirectToMobilePage的问题 使用Visio 2003 Drawing Control开发应用(3)(4) 使用Visio 2003 Drawing Control开发应用(2) 使用Visio 2003 Drawing Control开发应用(1)
处理大型xml文件
什么都不知道 · 2004-09-01 · via 博客园 - 什么都不知道

看到《修改大型 XML 文件的有效方法》http://www.microsoft.com/china/MSDN/library/data/xml/largexml.mspx这篇文章已经有一段时间了,今天终于腾出手来试了试。

都知道XmlDocument装载一个xml文件是将所有内容一下子load进来的,遇到很大的xml文件,问题就很大,曾经我load一个3mb的文件,调试时能明显感觉到消耗的时间。XmlReader好,据说XmlReader只是使用几十K(?)的内存,顺序读进来的,可是是只读的。

msdn这篇文章就是讲如何用XmlReader和XmlWriter来处理大型xml文件的。我写了如下的代码测试了一下,中间省去了一些这篇文章使用的XmlNameTable和XmlValidatingReader等。

这个是用XmlReader和XmlWriter处理
  private void button1_Click(object sender, System.EventArgs e)
  {
   System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString());
   File.Copy("ResourceList.xml", "Temp.xml", true);
// 因为要更新,先copy一个副本
   XmlTextReader reader = new XmlTextReader("Temp.xml");
   reader.WhitespaceHandling = WhitespaceHandling.Significant;
   reader.MoveToContent();

   StreamWriter sw = new StreamWriter("ResourceList.xml", false, System.Text.Encoding.UTF8); // 创建writer
   XmlWriter xw = new XmlTextWriter(sw);
   xw.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
   reader.Read();
   do
   {
    if(reader.Name == "RESOURCE")
     xw.WriteStartElement("RESOURCE");
    else if(reader.Name == "CTNO")
     xw.WriteElementString("CTNO", reader.ReadString());
    else if(reader.Name == "ITMNUM")
     xw.WriteElementString("ITMNUM", reader.ReadString());
    else if(reader.Name == "RESTYP")
     xw.WriteElementString("RESTYP", reader.ReadString());
    else if(reader.Name == "RESNAM")
    {
     if(reader.ReadString().Equals("I99000000001")) // 这里做比较,并替换内容
      xw.WriteElementString("RESNAM", "Hello");
     else
      xw.WriteElementString("RESNAM", reader.ReadString());
    }
    reader.Read();   
    if(reader.Name == "RESOURCE")
    {
     if(reader.NodeType == XmlNodeType.EndElement)
     {
      xw.WriteEndElement();
      reader.Read();
     }
    }
   }while(reader.NodeType == XmlNodeType.Element);

   xw.Close();
   reader.Close();
   System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString());
  }


下面是用XmlDocument来处理这个文件
  private void button2_Click(object sender, System.EventArgs e)
  {
   System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString());
   XmlDocument domTemp = new XmlDocument();
   domTemp.Load("ResourceList.xml");
   XmlNode node = domTemp.SelectSingleNode("descendant::RESOURCE[RESNAM='I99000000001']");
   if(node != null)
   {
    node.SelectSingleNode("RESNAM").InnerText = "Hello";
   }
   else
   {
    node = domTemp.SelectSingleNode("descendant::RESOURCE[RESNAM='Hello']");
    if(node != null)
     node.SelectSingleNode("RESNAM").InnerText = "I99000000001";
   }
   domTemp.Save("ResourceList.xml");
   System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString());
  }

我的测试用.net framework 1.1,xml文件大小808KB。
执行结果:
内存消耗:因为我不怎么会用CLR Profiler,不会分析 , 按理说肯定会有不同
执行时间:用xmlreader+xmlwriter,耗时370ms; 用xmlDocument耗时391ms