LINQ to XML X-DOM 内部实现

发布于 2024-10-26 19:53:45 字数 197 浏览 1 评论 0原文

System.Xml.Linq 命名空间中的 LINQ to XML X-DOM 在内部是如何实现的? (XNode、XElement 等)

它是否利用来自其他 XML 命名空间或其他名称的标准高性能单向 XmlReader/XmlWriter?

我问的原因是我试图找出在哪些情况下可以或应该使用,因为性能始终是一个问题。

How is the LINQ to XML X-DOM from the System.Xml.Linq namespace internally implemented? (XNode, XElement, etc.)

Is it utilizing standard high-performing one-way XmlReader/XmlWriter from the other XML namespaces or something else?

The reason I'm asking is that I'm trying to figure out in which circumstances could or should be used as performance is always a concern.

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

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

发布评论

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

评论(1

情魔剑神 2024-11-02 19:53:45

使用 Reflector(或者,当不再免费时,ILSpy :);不,我不是员工 - 只是偷偷传播消息!)似乎所有加载/保存方法都通过 XmlReaderXmlWriter 进行传输。
例如,XElement 的 Load(Stream, LoadOptions) 实现是这样做的:

public static XElement Load(Stream stream, LoadOptions options)
{
  XmlReaderSettings xmlReaderSettings = XNode.GetXmlReaderSettings(options);
  using (XmlReader reader = XmlReader.Create(stream, xmlReaderSettings))
  {
    return Load(reader, options);
  }
}

对于所有其他静态方法(包括 Parse)来说,情况都是类似的。

但是还有 XStreamingElement 构造函数 - 但是我在 XElement 类本身之外找不到它的任何实际用法。看起来这可能是一种优化的加载类型,但到目前为止,还没有被太多使用。

同样,Save 和 WriteTo 方法最终使用 XmlWriter 实例 - 例如:

public void Save(string fileName, SaveOptions options)
{
  XmlWriterSettings xmlWriterSettings = XNode.GetXmlWriterSettings(options);
  using (XmlWriter writer = XmlWriter.Create(fileName, xmlWriterSettings))
  {
    this.Save(writer);
  }
}

因此至少从性能的角度来看,它们从正确的类型开始:)

Using Reflector (or, when that's no longer free, ILSpy :); no I'm not an employee - just spreading the word surreptitiously!) it appears all the load/save methods channel through to XmlReader and XmlWriter.
For example - XElement's implementation of Load(Stream, LoadOptions) does this:

public static XElement Load(Stream stream, LoadOptions options)
{
  XmlReaderSettings xmlReaderSettings = XNode.GetXmlReaderSettings(options);
  using (XmlReader reader = XmlReader.Create(stream, xmlReaderSettings))
  {
    return Load(reader, options);
  }
}

And it's a similar story for all the other static methods - including Parse.

But then there is the XStreamingElement constructor - however I can't find any real usage of it outside of the XElement class itself. Looks like this could be an optimised type for loading that, as yet, isn't used by much.

Equally, the Save and WriteTo methods ultimately use an XmlWriter instance - e.g:

public void Save(string fileName, SaveOptions options)
{
  XmlWriterSettings xmlWriterSettings = XNode.GetXmlWriterSettings(options);
  using (XmlWriter writer = XmlWriter.Create(fileName, xmlWriterSettings))
  {
    this.Save(writer);
  }
}

So at least from a performance point of view they started with the right types :)

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