For the instance we have some IHttpHandler. where we have two armors: XmlElement xmlResponse - contains really big XML. HttpContext Context - The current HttpContext.
A simple solution:
context.Response.Write(xmlResponse.OwnerDocument.OuterXml);
but when the XML is really big, we can get an OutOfMemoryException on this line. The call stack will look like this:
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)
at System.IO.StringWriter.Write(Char value)
at System.Xml.XmlTextWriter.InternalWriteEndElement(Boolean longFormat)
at System.Xml.XmlTextWriter.WriteFullEndElement()
at System.Xml.XmlElement.WriteTo(XmlWriter w)
at System.Xml.XmlElement.WriteContentTo(XmlWriter w)
at System.Xml.XmlElement.WriteTo(XmlWriter w)
at System.Xml.XmlElement.WriteContentTo(XmlWriter w)
at System.Xml.XmlElement.WriteTo(XmlWriter w)
at System.Xml.XmlDocument.WriteContentTo(XmlWriter xw)
at System.Xml.XmlDocument.WriteTo(XmlWriter w)
at System.Xml.XmlNode.get_OuterXml()
I would write code like this:
HttpWriter writer = new HttpWriter(context.Response);
XmlTextWriter xmlWriter = new XmlTextWriter(writer);
xmlResponse.OwnerDocument.WriteTo(xmlWriter);
But the problem is that the HttpWriter constructor is internal! The HttpContext uses the HttpWriter inside the Write methods.
What exists around the desktop (other than creating an HttpWriter through reflection)?
source
share