English 中文(简体)
如何使用 Xml 文件而不将整个文档装入内存?
原标题:How to work with an Xml file without loading the whole document in memory?
  • 时间:2012-05-23 11:48:07
  •  标签:
  • c#
  • xml

如何添加一个新的节点, 更新现有的节点, 并删除 xml 文档的现有节点, 不将整个文档装入内存?

我有一个xml文件, 并把它当作我的应用程序的记忆,

它的结构是这样的:

<spiderMemory>
  <profileSite profileId="" siteId="">
    <links>
      <link>
        <originalUrl></originalUrl>
        <isCrawled></isCrawled>
        <isBroken></isBroken>
        <isHtmlPage></isHtmlPage>
        <firstAppearedLevel></firstAppearedLevel>
      </link>
    </links>
  </profileSite>
</spiderMemory>

XDocument怎么会这样呢?

谢谢 谢谢

问题回答

如果你想快速读数以百计的读数和写数... 你可能会使用错误的技术。 你有没有尝试过使用普通的 RDBMS?

如果您仍然需要 XML 代表,那么您可以创建导出方法,从数据库中生成 XML 。

XML并不能很好地替代这种问题 只是说说而已

还有... 在记忆中拥有整个东西有什么错? 它能有多大? 说1GB? 把它吸起来。说1TB? 哎呀。那么XML就是错误的,错误的,反之亦然。 (笑声) 那样的话, XML是错的,错误的,反之亦然。 (掌声)太夸张了!

您可以使用 XmlReader, 类似的东西 :

FileStream stream = new FileStream("test.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(stream);
while(reader.Read())
{
  Console.WriteLine(reader.Value);
}

这里是一个更精细的例子 http://msdn.microsoft.com/en-us/library/cc189056%28v=vs.95%29.aspx

正如Daren Thomas所说, 正确的解决方案是使用 RDBMS 而不是 XML 来满足您的需要。 我使用 XML 和 Java 有部分解决方案 。 < strong> sat Extraceer 不在记忆中分析整个文档, 比 DOM ( 仍然使用 XML 解析总是慢得多 ) 。 拖动分析器( 如 Stax) 允许u 控制被解析的内容 。 一个不那么干净的方法是当您获得所需的元素时在 SAX 解析器中丢出一个例外 。

要修改,最简单(但慢)的方式是使用 XPath 。 另一个( 未测试的)选项是将 XML 文件作为 < strong> text 处理, 然后 < strong > 搜索并替换 < /strong > 的东西。 您可以在这里使用各种文本搜索优化 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签