How to return XML to ASP.NET

This is a very simple question. I just want to learn about ASP.NET (C #). I used to do classic ASP and PHP.

For this project, I have ASP.NET 2.0 .

I have a web form with jqGrid Datagrid that I want to pass XML data through AJAX. jqGrid is not a problem here. "Problem" is the approach I should use to generate XML.

How to do it in ASP.NET?

  • Create a new web form that generates this data?
  • Am I using a new web service (which will not return the required XML document to me?)?
  • How do I put a function in an existing web form that shows a table? If so, how?

After making this decision: how can I output XML? I do not want to use any ASP.NET XML components because it is simple, simplified XML with one record after another. Using System.Xml would be too big to justify here.

<?xml version='1.0' encoding='utf-8'?> <rows> <page>1</page> <total>25</total> <records>3</records> <row id='1'> <cell>Row 1, Column 1</cell> <cell>Row 1, Column 2</cell> <cell>Row 1, Column 3</cell> </row> <row id='2'> <cell>Row 2, Column 1</cell> <cell>Row 2, Column 2</cell> <cell>Row 2, Column 3</cell> </row> <row id='3'> <cell>Row 3, Column 1</cell> <cell>Row 3, Column 2</cell> <cell>Row 3, Column 3</cell> </row> </rows> 

From my previous experience with other scripting languages, I just wanted to print an XML tag stream (Response.Write). Will I go to hell if I do this in ASP.NET?

+1
xml ajax architecture service
Dec 17 '08 at 13:33
source share
4 answers

Another solution for using full-blown XmlDocument / Linq in ashx is to use XmlWriter, a kind of intermediate solution between manually processing xml as a string and using dom.

Something like:

 StringBuilder query = new StringBuilder(); using (XmlWriter xmlWriter = XmlWriter.Create(query)) { xmlWriter.WriteStartElement("rows"); xmlWriter.WriteElementString("page", "1"); foreach(...) { xmlWriter.WriteStartElement("rows"); xmlWriter.WriteAttributeString("row", "1"); foreach(...) { xmlWriter.WriteElementString("cell", "Row 1, Column 1"); } xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); } 
+4
Dec 17 '08 at 14:15
source share

Using System.Xml (or System.Xml.Linq) will be less overhead in terms of its correctness than manually coding Response.Write and make sure that you specify everything you need, understand the encoding, etc. Why do you want to invent a wheel?

Create a new ashx instead of an aspx page, create the appropriate XmlDocument / XDocument and write the answer, setting the appropriate content type.

See this article for an example of creating an RSS feed. This is the same as on my site to generate RSS from a database - using LINQ to XML creates the entire document in one (albeit large, but readable) statement. Then this is just a case of setting the content type and calling context.Response.Write(doc) . Very simple.

+7
Dec 17 '08 at 13:38
source share

Hmm, I would like to convey two "accepted answers";)

John Skeet answered the first part of my question:

Create a new ashx instead of an aspx page

So, I created the generic handler that I was looking for.

Matthew Pelser answered the second part of my question:

use XmlWriter, which seems to be half the solution between manually processing xml as a string and using dom.

So, I used XmlWriter, and this is what I was looking for.

+1
Dec 17 '08 at 15:41
source share

working example test.ashx:

 <%@ WebHandler Class="XMLHandler" %> Imports System.Xml Public Class XMLHandler : Implements IHttpHandler Public Sub ProcessRequest(ByVal context as HttpContext) Implements IHttpHandler.ProcessRequest context.Response.ContentType = "text/xml" Dim input as string = context.Request.Params("input") Dim settings as new XmlWriterSettings() settings.Indent = false Using xml as XmlWriter = XmlWriter.Create(context.Response.Output, settings) xml.WriteStartDocument xml.WriteStartElement("wsis") xml.WriteAttributeString("id","p4") xml.WriteElementString("user_id", "test") xml.WriteEndElement() xml.WriteEndDocument() End Using End Sub Public ReadOnly Property IsReusable() as Boolean Implements IHttpHandler.IsReusable Get Return True End Get End Property End Class 
0
Jan 27 '16 at 10:26
source share



All Articles