XmlTextReader 与 XDocument

发布于 2024-12-15 04:17:21 字数 258 浏览 1 评论 0 原文

我能够在.NET 中解析XML。现在我至少可以在 XmlTextReaderXDocument 之间进行选择。这两者(或框架中包含的任何其他 XML 解析器)之间是否有任何比较?

也许这可以帮助我做出决定,而无需深入尝试两者。

XML 文件预计会相当小,与易用性相比,速度和内存使用只是一个小问题。 :-)

(我将在 C# 和/或 IronPython 中使用它们。)

谢谢!

I'm in the position to parse XML in .NET. Now I have the choice between at least XmlTextReader and XDocument. Are there any comparisons between those two (or any other XML parsers contained in the framework)?

Maybe this could help me to decide without trying both of them in depth.

The XML files are expected to be rather small, speed and memory usage are a minor issue compared to easiness of use. :-)

(I'm going to use them from C# and/or IronPython.)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2024-12-22 04:17:21

如果您愿意将所有内容读入内存,请使用 XDocument。它会让您的生活更加更加轻松。 LINQ to XML 是一个可爱的API。

基本上,如果您需要以流方式处理巨大的 XML 文件,请使用 XmlReader(例如 XmlTextReader)。这是一个更痛苦的 API,但它允许流式传输(即仅在需要时处理数据,因此您可以浏览一个巨大的文档,但一次只能在内存中保留少量数据)。

然而,有一种混合方法 - 如果您有一个由小元素组成的巨大文档,您可以从位于元素开头的 XmlReader 创建一个 XElement,处理使用 LINQ to XML 编辑该元素,然后将 XmlReader 移动到下一个元素并重新开始。

If you're happy reading everything into memory, use XDocument. It'll make your life much easier. LINQ to XML is a lovely API.

Use an XmlReader (such as XmlTextReader) if you need to handle huge XML files in a streaming fashion, basically. It's a much more painful API, but it allows streaming (i.e. only dealing with data as you need it, so you can go through a huge document and only have a small amount in memory at a time).

There's a hybrid approach, however - if you have a huge document made up of small elements, you can create an XElement from an XmlReader positioned at the start of the element, deal with the element using LINQ to XML, then move the XmlReader onto the next element and start again.

心的憧憬 2024-12-22 04:17:21

XmlTextReader 已被弃用,请勿使用它。

  1. 来自 XmlTeam 的 msdn 博客

    有效的 Xml 第 1 部分:选择正确的 API

    <块引用>

    避免使用XmlTextReader。它包含相当多的错误,如果不破坏已经使用它的现有应用程序,就无法修复这些错误。

    世界已经进步,不是吗?您应该避免使用的 Xml API。

    <块引用>

    过时的 API 很容易出现,因为编译器可以帮助识别它们,但您还应该避免使用另外两个 API,即 XmlTextReaderXmlTextWriter。我们在这些类中发现了许多错误,我们无法在不破坏现有应用程序的情况下修复这些错误。最简单的方法是弃用这些类并要求人们使用替换 API。不幸的是,这两个类不能被标记为过时,因为它们是 ECMA-335(公共语言基础设施)标准的一部分(http://www.ecma-international.org/publications/standards/Ecma-335.htm) – 配套的 CLILibrary.xml 文件是分区 IV 的一部分)。

    好消息是,即使这些类没有被弃用,.NET Framework 中已经有这些类的替代 API,并且迁移到它们相对容易。首先,有必要找到使用 XmlTextReaderXmlTextWriter 的位置(不幸的是,这是一个手动步骤)。现在,所有出现的 XmlTextReader 都应替换为 XmlReader,所有出现的 XmlTextWriter 应替换为 XmlWriter (请注意,XmlTextReader 派生自 XmlReaderXmlTextWriter 派生自 XmlWriter,因此应用程序已经可以使用这些例如作为形式参数)。最后一步是更改 XmlReader/XmlWriter 对象的实例化方式 - 不需要直接创建读取器/写入器,而是需要静态工厂方法 。 Create() 存在于 XmlReaderXmlWriter API 上。

  2. 此外,Visual Studio 中的智能感知不会在 System.Xml 命名空间下列出 XmlTextReader。该类定义为:

    [EditorBrowsable(EditorBrowsableState.Never)]
    公共类 XmlTextReader :XmlReader、IXmlLineInfo、IXmlNamespaceResolver
    

XmlReader.Create 工厂方法根据传递的设置返回抽象类 XmlReader 的其他内部实现。


对于只进流 API(即不会将整个内容加载到内存中),请通过 XmlReader.Create 方法使用 XmlReader

要使用更简单的 API,请使用 XDocument 又名 LINQ To XML。在此处查找 XDocumentXmlDocument此处

XmlTextReader is kind of deprecated, do not use it.

  1. From msdn blogs by XmlTeam

    Effective Xml Part 1: Choose the right API

    Avoid using XmlTextReader. It contains quite a few bugs that could not be fixed without breaking existing applications already using it.

    The world has moved on, have you? Xml APIs you should avoid using.

    Obsolete APIs are easy since the compiler helps identifying them but there are two more APIs you should avoid using – namely XmlTextReader and XmlTextWriter. We found a number of bugs in these classes which we could not fix without breaking existing applications. The easy route would be to deprecate these classes and ask people to use replacement APIs instead. Unfortunately these two classes cannot be marked as obsolete because they are part of ECMA-335 (Common Language Infrastructure) standard (http://www.ecma-international.org/publications/standards/Ecma-335.htm) – the companion CLILibrary.xml file which is a part of Partition IV).

    The good news is that even though these classes are not deprecated there are replacement APIs for these in .NET Framework already and moving to them is relatively easy. First it is necessary to find the places where XmlTextReader or XmlTextWriter is being used (unfortunately it is a manual step). Now all the occurrences of XmlTextReader should be replaced with XmlReader and all the occurrences of XmlTextWriter should be replaced with XmlWriter (note that XmlTextReader derives from XmlReader and XmlTextWriter derives from XmlWriter so the app can already be using these e.g. as formal parameters). The last step is to change the way the XmlReader/XmlWriter objects are instantiated – instead of creating the reader/writer directly it is necessary to the static factory method .Create() present on both XmlReader and XmlWriter APIs.

  2. Furthermore, intellisense in Visual Studio doesn't list XmlTextReader under System.Xml namespace. The class is defined as:

    [EditorBrowsable(EditorBrowsableState.Never)]
    public class XmlTextReader : XmlReader, IXmlLineInfo, IXmlNamespaceResolver
    

The XmlReader.Create factory methods return other internal implementations of the abstract class XmlReader depending on the settings passed.


For forward-only streaming API (i.e. that doesn't load the entire thing into memory), use XmlReader via XmlReader.Create method.

For an easier API to work with, go for XDocument aka LINQ To XML. Find XDocument vs XmlDocument here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文