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

推荐订阅源

T
Tenable Blog
H
Heimdal Security Blog
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Schneier on Security
G
GRAHAM CLULEY
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
C
Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
P
Proofpoint News Feed
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
S
Security Affairs
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
N
News | PayPal Newsroom
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
NISL@THU
NISL@THU

博客园 - 什么都不知道

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