How to return XML in ASP.NET?

前端 未结 9 1416
粉色の甜心
粉色の甜心 2020-11-28 20:17

I have encountered many half-solutions to the task of returning XML in ASP.NET. I don\'t want to blindly copy & paste some code that happens to work most of the time, th

相关标签:
9条回答
  • 2020-11-28 20:38

    You've basically answered anything and everything already, so I'm no sure what the point is here?

    FWIW I would use an httphandler - there seems no point in invoking a page lifecycle and having to deal with clipping off the bits of viewstate and session and what have you which don't make sense for an XML doc. It's like buying a car and stripping it for parts to make your motorbike.

    And content-type is all important, it's how the requester knows what to do with the response.

    0 讨论(0)
  • 2020-11-28 20:39
    XmlDocument xd = new XmlDocument();
    xd.LoadXml(xmlContent);
    
    context.Response.Clear();
    context.Response.ContentType = "text/xml";
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    xd.Save(context.Response.Output);
    context.Response.Flush();
    context.Response.SuppressContent = true;
    context.ApplicationInstance.CompleteRequest();
    
    0 讨论(0)
  • 2020-11-28 20:44

    Below is an example of the correct way I think. At least it is what I use. You need to do Response.Clear to get rid of any headers that are already populated. You need to pass the correct ContentType of text/xml. That is the way you serve xml. In general you want to serve it as charset UTF-8 as that is what most parsers are expecting. But I don't think it has to be that. But if you change it make sure to change your xml document declaration and indicate the charset in there. You need to use the XmlWriter so you can actually write in UTF-8 and not whatever charset is the default. And to have it properly encode your xml data in UTF-8.

       ' -----------------------------------------------------------------------------
       ' OutputDataSetAsXML
       '
       ' Description: outputs the given dataset as xml to the response object
       '
       ' Arguments:
       '    dsSource           - source data set
       '
       ' Dependencies:
       '
       ' History
       ' 2006-05-02 - WSR : created
       '
       Private Sub OutputDataSetAsXML(ByRef dsSource As System.Data.DataSet)
    
          Dim xmlDoc As System.Xml.XmlDataDocument
          Dim xmlDec As System.Xml.XmlDeclaration
          Dim xmlWriter As System.Xml.XmlWriter
    
          ' setup response
          Me.Response.Clear()
          Me.Response.ContentType = "text/xml"
          Me.Response.Charset = "utf-8"
          xmlWriter = New System.Xml.XmlTextWriter(Me.Response.OutputStream, System.Text.Encoding.UTF8)
    
          ' create xml data document with xml declaration
          xmlDoc = New System.Xml.XmlDataDocument(dsSource)
          xmlDoc.DataSet.EnforceConstraints = False
          xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
          xmlDoc.PrependChild(xmlDec)
    
          ' write xml document to response
          xmlDoc.WriteTo(xmlWriter)
          xmlWriter.Flush()
          xmlWriter.Close()
          Response.End()
    
       End Sub
       ' -----------------------------------------------------------------------------
    
    0 讨论(0)
提交回复
热议问题