PROWAREtech
ASP.NET: XML Output
Output XML in an ASPX page (.NET Framework).
Output from Database Example (XmlWriter Class)
This code will connect to the MS SQL Server pubs database, open the authors table, and output the results in the Response.OutputStream and the browser will save the XML file as authors.xml. This uses the XmlWriter class.
Use Linq to XML to query this XML document.
<%@ Page Language="VB" ContentType="text/xml" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Configuration" %>
<%
Response.Clear()
'have the browser download the xml output as a file named "authors.xml"
Response.AppendHeader("content-disposition", "attachment;filename=authors.xml")
Dim oCon As New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ConnectPubs").ToString())
oCon.Open()
'FROM THE pubs DATABASE IN MS SQL SERVER
Dim oCmd As New OleDbCommand("SELECT au_id,phone,au_lname,au_fname FROM authors", oCon)
Dim oRdr As OleDbDataReader = oCmd.ExecuteReader() 'open a datareader to read the table data
Dim xmlSett As New XmlWriterSettings()
xmlSett.NewLineOnAttributes = True 'make the XML file easy to read
xmlSett.Indent = True 'make the XML file even easier to read
Using xmlWrit As XmlWriter = XmlWriter.Create(Response.OutputStream, xmlSett)
xmlWrit.WriteStartDocument()
'table name - can be any name but this element is important to include
xmlWrit.WriteStartElement("authors")
Do While oRdr.Read()
xmlWrit.WriteStartElement("author") 'begin author
xmlWrit.WriteStartAttribute("ssn") 'begin ssn attribute
xmlWrit.WriteValue(oRdr(0))
xmlWrit.WriteEndAttribute() 'end ssn attribute
xmlWrit.WriteStartElement("phone") 'begin phone
xmlWrit.WriteValue(oRdr(1))
xmlWrit.WriteEndElement() 'end phone
xmlWrit.WriteStartElement("lastname") 'begin lastname
xmlWrit.WriteValue(oRdr(2))
xmlWrit.WriteEndElement() 'end lastname
xmlWrit.WriteStartElement("firstname") 'begin firstname
xmlWrit.WriteValue(oRdr(3))
xmlWrit.WriteEndElement() 'end firstname
xmlWrit.WriteEndElement() 'end author
Loop
xmlWrit.WriteEndElement() 'end authors
xmlWrit.WriteEndDocument()
xmlWrit.Close()
End Using
oRdr.Close()
oCon.Close()
Response.End()
%>
XmlReader Class
This code reads the entire authors.xml file created above. It uses an HtmlTable for formatting.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
XmlReader reader = XmlReader.Create(Server.MapPath("authors.xml"));
while(reader.Read())
{
switch(reader.NodeType)
{
case XmlNodeType.Text:
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell td = new HtmlTableCell();
td.InnerText = reader.Value;
tr.Cells.Add(td);
tbl.Rows.Add(tr);
}
break;
case XmlNodeType.Element:
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell td = new HtmlTableCell();
td.InnerHtml = "<b>" + reader.Name + "</b>";
tr.Cells.Add(td);
tbl.Rows.Add(tr);
}
for(int i = 0; i < reader.AttributeCount; i++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell td = new HtmlTableCell();
td.InnerText = reader.GetAttribute(i);
tr.Cells.Add(td);
tbl.Rows.Add(tr);
}
break;
}
}
}
</script>
<html>
<head>
<title>XmlReader Example</title>
</head>
<body>
<table id="tbl" runat="server"></table>
</body>
</html>
Comment