How to efficiently send a large XML client to ASP.NET?

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)?

+3
source share
3 answers
xmlResponse.OwnerDocument.Save(Response.Output);
+1
source

. OutputStream

XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, 
System.Text.Encoding.UTF8);  
+3

, , , . OutOfMemory .

But if these are huge XML files, using HttpResponse and "writing" is not the best option. It is better to create and link and allow something other than the .NET Response object to deliver files to the user. Therefore, I would seriously consider rethinking the architecture that you are designing. Perhaps a file transfer web service might be better off transferring these large files? Just trying to post some ideas.

Am I missing something?

0
source

Source: https://habr.com/ru/post/1704651/


All Articles