【发布时间】:2010-11-25 09:31:17
【问题描述】:
我有一个包含大约 120MB 数据的 XElement 对象。 XML 由大约 6000 个元素组成,每个元素大约 20kb。
我正在尝试调用 XElement.ToString(),因为我需要在 Web 服务中返回 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() 的替代方案,它会减少内存密集度吗?或者我可以设置一些允许更大内存空间的参数?
【问题讨论】:
-
您需要通过网络服务返回 120mb 的数据...
-
我就是这么想的……
-
尝试压缩数据并返回
-
这是一个内部网络服务,仅用于在两个独立的内部系统之间传输数据。通常要么在同一台服务器上,要么在最坏的情况下并排在一个机架上。
-
如果是用于内部系统,我仍然不会为此使用 Web 服务。如果网络上有任何类型的负载,那么请求将保持打开的时间过长。为什么你不能在网络上通过 FTP 传输它或将它推到某个共享目录中?或 HTTP 请求以外的其他内容。