- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个包含大约 120MB 数据的 XElement
对象。 XML 由大约 6000 个元素组成,每个元素约 20kb。
我正在尝试调用 XElement.ToString()
,因为我需要在网络服务中返回 OuterXml。
我得到一个 System.OutOfMemoryException
。
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.String.GetStringForStringBuilder(String value, Int32 startIndex, Int32 length, Int32 capacity)
at System.Text.StringBuilder.GetNewString(String currentString, Int32 requiredLength)
at System.Text.StringBuilder.Append(Char[] value, Int32 startIndex, Int32 charCount)
at System.IO.StringWriter.Write(Char[] buffer, Int32 index, Int32 count)
at System.Xml.XmlEncodedRawTextWriter.FlushBuffer()
at System.Xml.XmlEncodedRawTextWriter.WriteAttributeTextBlock(Char* pSrc, Char* pSrcEnd)
at System.Xml.XmlEncodedRawTextWriter.WriteString(String text)
at System.Xml.XmlEncodedRawTextWriterIndent.WriteString(String text)
at System.Xml.XmlWellFormedWriter.WriteString(String text)
at System.Xml.XmlWriter.WriteAttributeString(String prefix, String localName, String ns, String value)
at System.Xml.Linq.ElementWriter.WriteStartElement(XElement e)
at System.Xml.Linq.ElementWriter.WriteElement(XElement e)
at System.Xml.Linq.XElement.WriteTo(XmlWriter writer)
at System.Xml.Linq.XNode.GetXmlString(SaveOptions o)
at System.Xml.Linq.XNode.ToString()
我在 XmlDocument
中有相同的数据,可以毫无问题地调用 XmlDocument.OuterXml
。我还可以调用 XElement.Save()
将 XML 毫无问题地保存到文件中。
谁能建议 XElement.ToString() 的内存占用较少的替代方法?或者我可以设置一些参数以允许更大的内存空间?
最佳答案
听起来你在那里写的数据方式太多了; 通常原始XmlWriter
可能是本卷的最佳选择。但是,如果您可以 Save()
成功,您或许可以尝试:
string xml;
using(var sw = new StringWriter()) {
el.Save(sw);
xml = sw.ToString();
}
或者也许:
string xml;
using (var ms = new MemoryStream()) {
using(var tw = new StreamWriter(ms, Encoding.UTF8))
{
el.Save(tw);
}
xml = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}
但其中任何一个(或两者)都可能会在一阵 Spark 中爆炸。您可能还想调查 XStreamingElement
这是为这种情况设计的...但是,这仍然是很多 xml - 特别是对于网络服务。您愿意接受其他(更密集的)序列化格式的建议吗?
关于c# - XElement.ToString() 导致 System.OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4275506/
我已经花了几周的时间来解决这个问题。但我仍然无法解决这个问题。 我在 angularjs 中使用 http 调用 Web API 服务 $http({
我的转储文件包含 OutOfMemoryException 但所有对象字段似乎都为空。这只是一个误报还是有办法知道这个异常(exception)? 0:052> !do 000000027fff10e
我使用的项目包含许多从单个基类继承的类。在单元测试中,我需要按类型和数据比较收到的结果。 当我在条件列表包含足够多的不同条件的情况下使用类型匹配比较时,编译器会抛出 OutOfMemoryExcept
我的应用程序在 512mb 手机模拟器上运行时突然崩溃(可重现)(只有 920 来测试)。没有任何异常,它只是变黑了。然后我在应用程序上运行分析工具,发现内存在应用程序崩溃时达到了极限。应用程序退出并
我试图弄清楚应该如何使用 MemoryCache 以避免出现内存不足异常。我来自 ASP.Net 背景,缓存管理它自己的内存使用,所以我希望 MemoryCache 会做同样的事情。正如我制作的波纹管
我从某个生产代码版本中获得了异常日志。 System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was
比如说,我们有一个表,其中包含一些包含 jpg 文件二进制数据的大型 text 字段。任务是从磁盘上的数据库中获取这些文件。因此,起初我决定执行以下操作: MyDataContext dc = new
在 Windows 窗体 C# 应用程序中,我有一个文本框,用户可以在其中粘贴日志数据,并对其进行排序。我需要单独检查每一行,所以我按新行拆分输入,但如果有很多行,超过 100,000 行左右,它会抛
我们正在将一个大的平面文件加载到 BizTalk Server 2006(原始版本,不是 R2)中 - 大约 125 MB。我们针对它运行一个映射,然后获取每一行并调用一个存储过程。 我们在编排处理期
比如说,我们有一个表,其中包含一些包含 jpg 文件二进制数据的大型 text 字段。任务是从磁盘上的数据库中获取这些文件。因此,起初我决定执行以下操作: MyDataContext dc = new
在我的应用程序中,用户可以启动相机 Intent 并拍照并将其保存到自定义文件夹中。然后他们可以重新启动相机拍摄另一张照片等。因此最终用户可以拍摄多张照片。 我遇到的问题是,在我的画廊中,当我尝试将所
在 Windows 窗体 C# 应用程序中,我有一个文本框,用户可以在其中粘贴日志数据,并对其进行排序。我需要单独检查每一行,所以我按新行拆分输入,但如果有很多行,超过 100,000 行左右,它会抛
我收到 OutOfMemoryException,而堆似乎仍然有足够的剩余空间。 我自己的日志报告 - VM 内存使用了 48G 中的 22.28G 来自-XX:+PrintGCDetails -XX
给定一个简单的案例类 case class Rating(user: Int, item: Int, rating: Double) 和两个RDD[Rating]:我们得到的 OOME 在两个 R
我正在开发一个应用程序,该应用程序使用移动设备拍照并使用网络服务发送照片。但是在我拍了 4 张照片之后,我在下面的代码中得到了一个 OutOfMemoryException。我尝试调用 GC.Coll
我想调整我网站中图片的大小,但是当我使用 Bitmap 加载 14032*19864(png 扩展名)的图片时,抛出 OutOfMemoryException。我的编译器配置是 any cpu。我在怀
在我的应用程序中,我正在使用 Windows 服务读取文件。该服务每秒读取一次文件。这是我用来读取文件的代码: public static byte[] GetBytesFromFile(string
我正在尝试将用户的整个桌面捕获为图像。我通过以下方式执行此操作: public Bitmap CaptureScreen() { // Set up a bit
我正在开发一个 64 位 .Net Windows 服务应用程序,该应用程序本质上加载了一堆数据以供处理。在执行数据量测试时,我们能够压倒该进程并抛出 OutOfMemoryException(我没有
我知道我的问题没有简单的答案,但我会很感激想法、指南或某种要看的东西列表 我有一个不断抛出 OutOfMemoryException 异常的网络 Windows 服务。该服务有两个针对 x86 和 x
我是一名优秀的程序员,十分优秀!