views:

1242

answers:

3

This is a very basic question. I'm just on my mission to learn ASP.NET (C#). I've done classic ASP and PHP before.

For this project I have ASP.NET 2.0 at my hands.

I have a Web Form that has a jqGrid Datagrid that I want to feed XML data via AJAX. jqGrid is not the problem here, though. The "problem" is the approach that I should take to generate the XML.

How do I do that in ASP.NET?

  • Do I create a new Web Form that generates that data?
  • Do I use a new Web Service (that would not return the XML I need, would it?)?
  • Do I somehow put a function in the existing Web Form that shows the table? If so, how?

After that decision is made: How do I output the XML? I don't want to use any XML components of ASP.NET, because it's just plain, simplistic XML with one record after each other. Using System.Xml would be too much overhead to be justified 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 the other scripting languages, I'd just like to print out a stream of XML tags (Response.Write). Will I go to hell if I do so in ASP.NET?

+6  A: 

Using System.Xml (or System.Xml.Linq) would be less overhead in terms of getting it right than hand-coding Response.Write and making sure you quote everything you need to, sort out the encoding etc. Why would you want to reinvent the wheel?

Create a new ashx instead of aspx page, create the appropriate XmlDocument/XDocument and write that to the response, having set the content type appropriately.

See this article for an example creating an RSS feed. It's the same type of thing as I do on my site to generate RSS from the database - using LINQ to XML this creates the whole document in a single (admittedly large, but readable) statement. Then it's just a case of setting the content type and calling context.Response.Write(doc). Very simple.

Jon Skeet
Okay, I need to add: For this project I only have .NET 2.0
BlaM
The article is in .NET 2.0
Eduardo Campañó
That's true. I can't use LINQ, though. ;)
BlaM
+1  A: 

Another solution to using a full blown XmlDocument/Linq in the ashx is to use the XmlWriter, sort of a halfway solution between hand crafting the xml as a string and using a 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();

}

Matthew Pelser
+1  A: 

Hm, I'd like to hand out two "accepted answers". ;)

Jon Skeet answered the first part of my question:

Create a new ashx instead of aspx page

So I've created a generic handler which is what I was looking for.

Matthew Pelser answered the second part of my question:

use the XmlWriter, sort of a halfway solution between hand crafting the xml as a string and using a dom.

So I've used the XmlWriter, which is what I was looking for.

BlaM