C#XSLT转换内存不足 [英] C# XSLT Transformation Out of Memory

查看:151
本文介绍了C#XSLT转换内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,



我对改造使用XSLT XML文档下面的代码。
问题是,当XML文档是12MB左右的C#内存用完。
是否有这样做不会消耗那么多的内存变换不同的方式?



 公共字符串变换(XPathDocument中myXPathDoc, XslCompiledTransform myXslTrans)
{

{
变种STM =新的MemoryStream();
myXslTrans.Transform(myXPathDoc,空,STM);
变种SR =新的StreamReader(STM);
返回sr.ReadToEnd();
}
赶上(例外五)
{
//记录异常
}
}

下面是堆栈跟踪:

 。 String.GetStringForStringBuilder(字符串值,的Int32的startIndex,长的Int32,的Int32容量)
在System.Text.StringBuilder.GetNewString(字符串currentString,的Int32 requiredLength)
在System.Text.StringBuilder.Append(CHAR [ ]值,的Int32的startIndex,的Int32 charCount)
在System.IO.StreamReader.ReadToEnd()
在变换(XPathDocument中myXPathDoc,XslCompiledTransform myXslTrans)


解决方案

我会做的第一件事就是隔离问题。把整个企业的MemoryStream出去游玩和流输出到文件,例如:

 使用(XR的XmlReader = XmlReader.Create (新的StreamReader(input.xml中)))使用
(XmlWriter的XW = XmlWriter.Create(新的StreamWriter(与Output.xml)))
{
xslt.Transform(XR ,XW);
}

如果你仍然可以获得内存外的一个例外(我敢打赌折叠钱,你会),这是一个非常公平的迹象表明,问题不是与输出的大小,而在于在改变自己,比如一些一些递归无限,如:

 <的xsl:模板匹配=foo的> 
<酒吧和GT;
< XSL:申请模板选择= />中。
< /酒吧和GT;
< / XSL:模板>


All,

I have the below code for Transforming an XML Document using an XSLT. The problem is when the XML Document is around 12MB the C# runs out of memory. Is there a different way of doing the transform without consuming that much memory?

public string Transform(XPathDocument myXPathDoc, XslCompiledTransform myXslTrans)
    {
        try
        {
            var stm = new MemoryStream();
            myXslTrans.Transform(myXPathDoc, null, stm);
            var sr = new StreamReader(stm);
            return sr.ReadToEnd();
        }
        catch (Exception e)
        {
          //Log the Exception
        }
    }

Here is the stack trace:

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.StreamReader.ReadToEnd()
at Transform(XPathDocument myXPathDoc, XslCompiledTransform myXslTrans)

解决方案

The first thing I would do is to isolate the problem. Take the whole MemoryStream business out of play and stream the output to a file, e.g.:

using (XmlReader xr = XmlReader.Create(new StreamReader("input.xml")))
using (XmlWriter xw = XmlWriter.Create(new StreamWriter("output.xml")))
{
   xslt.Transform(xr, xw);
}

If you still get an out-of-memory exception (I'd bet folding money that you will), that's a pretty fair indication that the problem's not with the size of the output but rather with something in the transform itself, e.g. something that recurses infinitely like:

<xsl:template match="foo">
   <bar>
      <xsl:apply-templates select="."/>
   </bar>
</xsl:template>

这篇关于C#XSLT转换内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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