Code Samples: ASP.NET

This example uses an SQL database and the System.Xml.XmlWriter class to generate the obituaries list. Examples are given in both C# and VB. Call this function to regenerate the XML file whenever an update is made to the database.

XML Generation: C#

using System.Data.SqlClient;
using System.Xml;

public class ObitsXML {

	public void write() {

		// Connect to the database
		SQLConnection myDB = new SqlConnection("user id=username;password=password;server=serverurl;" +
			"Trusted_Connection=yes;database=database;connection timeout=30");
		myDB.Open();


		// Set up the DB reader
		// You may wish to include a WHERE clause to filter the entries, but ORDER by makes no difference
		SqlDataReader myReader = null;
		SqlCommand myCmd = new SqlCommand("SELECT * FROM obituaries", myDB);


		// Start the XML file
		// Point the file to a public location on your web server
		using (XmlWriter myXML = XmlWriter.Create("obituaries.xml", Encoding.Unicode)) {

			// Document start and generated timestamp
			myXML.WriteStartDocument();
			myXML.WriteStartElement("obituaries");
			myXML.WriteAttributeString("generated", DateTime.Now.ToString());

			// Start reading in the data and writing it to the XML
			myReader = myCmd.ExecuteReader();
			while (myReader.Read()) {
				// XmlWriter will automatically escape data as needed (</> to &lt;/&gt;, & to &amp;, etc.)
				myXML.WriteStartElement("obit");
				myXML.WriteElementString("uniqID", myReader["id"].ToString());
				myXML.WriteElementString("firstName", myReader["firstName"].ToString());
				myXML.WriteElementString("lastName", myReader["lastName"].ToString());
				myXML.WriteElementString("middleInitial", myReader["middleInitial"].ToString());
				myXML.WriteElementString("deathDate", myReader["deathDate"].ToString());
				myXML.WriteElementString("city", myReader["city"].ToString());
				myXML.WriteElementString("state", myReader["state"].ToString());
				myXML.WriteElementString("bio", myReader["bio"].ToString());
				myXML.WriteElementString("visitation", myReader["visitation"].ToString());
				myXML.WriteElementString("service", myReader["service"].ToString());
				myXML.WriteElementString("photo", "http://mysite.com/images/obituaries/"+myReader["photo"].ToString());
				myXML.WriteElementString("link", "http://mysite.com/obituaries.asp?id="+myReader["id"].ToString());
				myXML.WriteEndElement();
			}

			// Close document
			myXML.WriteEndElement();
			myXML.WriteEndDocument();
		}


		// Close connections
		myReader.Close();
		myDB.Close();

	}
}

XML Generation: VB.NET

Imports System.Data.SqlClient
Imports System.Xml

Public Class ObitsXML

	Public Sub write()

		' Connect to the database
		Dim myDB As SQLConnection = New SqlConnection("user id=username;password=password;server=serverurl;" & _
			"Trusted_Connection=yes;database=database;connection timeout=30")
		myDB.Open()


		' Set up the DB reader
		' You may wish to include a WHERE clause to filter the entries, but ORDER by makes no difference
		Dim myReader As SqlDataReader = Nothing
		Dim myCmd As New SqlCommand("SELECT * FROM obituaries", myDB)


		' Start the XML file
		' Point the file to a public location on your web server
		Using myXML As XmlWriter = XmlWriter.Create("obituaries.xml", Encoding.Unicode)

			' Document start and generated timestamp
			myXML.WriteStartDocument()
			myXML.WriteStartElement("obituaries")
			myXML.WriteAttributeString("generated", DateTime.Now.ToString())

			' Start reading in the data and writing it to the XML
			myReader = myCmd.ExecuteReader()
			While myReader.Read()
				// XmlWriter will automatically escape data as needed (</> to &lt;/&gt;, & to &amp;, etc.)
				myXML.WriteStartElement("obit")
				myXML.WriteElementString("uniqID", myReader("id").ToString())
				myXML.WriteElementString("firstName", myReader("firstName").ToString())
				myXML.WriteElementString("lastName", myReader("lastName").ToString())
				myXML.WriteElementString("middleInitial", myReader("middleInitial").ToString())
				myXML.WriteElementString("deathDate", myReader("deathDate").ToString())
				myXML.WriteElementString("city", myReader("city").ToString())
				myXML.WriteElementString("state", myReader("state").ToString())
				myXML.WriteElementString("bio", myReader("bio").ToString())
				myXML.WriteElementString("visitation", myReader("visitation").ToString())
				myXML.WriteElementString("service", myReader("service").ToString())
				myXML.WriteElementString("photo", "http://mysite.com/images/obituaries/" & myReader("photo").ToString())
				myXML.WriteElementString("link", "http://mysite.com/obituaries.asp?id=" & myReader("id").ToString())
				myXML.WriteEndElement()
			End While

			' Close document
			myXML.WriteEndElement()
			myXML.WriteEndDocument()
		End Using


		' Close connections
		myReader.Close()
		myDB.Close()

	End Sub
End Class