XElement.ToString()导致的System.OutOfMemoryException [英] XElement.ToString() causes System.OutOfMemoryException

查看:897
本文介绍了XElement.ToString()导致的System.OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含有关数据的120MB的的XElement 对象。该XML包括各约20KB约6000元的。

I have an XElement object that contains about 120MB of data. The XML consists of approx 6000 elements of about 20kb each.

我想打电话给 XElement.ToString()因为我需要一个web服务返回OuterXml。

I am trying to call XElement.ToString() as I need to return the OuterXml in a webservice.

我得到一个的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到一个文件中没有问题。

I have the same data in an XmlDocument and can call XmlDocument.OuterXml without a problem. I can also call XElement.Save() to save the XML to a file without a problem.

任何人都可以建议XElement.ToString()的替代,将少占用大量内存?或者可以选择一些参数,我可以设置将允许更大的存储空间?

Can anyone suggest an alternative to XElement.ToString() that would be less memory intensive? Or alternatively some parameters I can set that would allow for a larger memory space?

推荐答案

这听起来像你写的办法的太多的数据有; 一般的原的XmlWriter 可能是此卷的最佳选择。但是,如果你能保存()成功也许你可以试试:

It sounds like you're writing way too much data there; generally raw XmlWriter might be the best option for this volume. However, if you can Save() successfully you could perhaps try:

    string xml;
    using(var sw = new StringWriter()) {
        el.Save(sw);
        xml = sw.ToString();
    }



或者

or maybe:

    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);
    }



但这些(或两者)仍有可能在一阵火花爆炸。您可能还需要调查 XStreamingElement 这是专为这种类型的场景......但尽管如此,这是一个很大的XML - 尤其作为一个Web服务。你是开放的替代(更密集)的建议serializiation格式?

But either (or both) of these might still explode in a shower of sparks. You might also want to investigate XStreamingElement which is designed for this type of scenario... but still, that is a lot of xml - especially for a web-service. Would you be open to suggestions of alternative (much denser) serializiation formats?

这篇关于XElement.ToString()导致的System.OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆