tags:

views:

60

answers:

3

Basically I have a asp.net that I need to display an XML document (straight xml, with the tree nodes) I have the sql statement which returns the row names and each one of their values. Just wondering how I should go about doing this?

SQL Server 2008, my query is just a select * from offices, my results is "1","New York","New York City","555-5555" I would like the output to be

<item id="1"> 
<state>New York</state> 
<city>New York City</city> 
<phone>555-5555</phone> 
</item> 

where state/city/phone is the column name, and the value is the value of that column

A: 

You can pass by datatable than a XML

ma7moud
+1  A: 
SELECT id AS '@id',state,city,phone FROM offices AS item
FOR XML PATH, ROOT('Offices')
Joakim
+2  A: 

You can select your query into a DataSet: (Taken from MSDN)

  string queryString = 
      "SELECT CustomerID, CompanyName FROM dbo.Customers";
    SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");

Then, you can write that DataSet to an XML string:

string xmlDS = custDS.GetXml();

View the MSDN pages for more options.

Jim Schubert
Okay this worked, now I have the table as an xml that looks something like <NewDataSet>\r\n <officelist>\r\n <OfficeID>2176</OfficeID>\r\n <Office>My Office </Office>\r\n <Region>Toronto</Region>\r\n <Division>TO </Division>\r\nHow do I get this to a strict xml page where you can open and close the nodes (in IE)
Spooks
You could write the string to the Response object: `Response.Clear(); Response.ContentType = "text/xml"; Response.Write(xmlDS.ToString()); Response.End();`
Jim Schubert