using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; using System.IO; namespace Samples { public partial class _Default: System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { GetUsernameList(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion private void GetUsernameList() { //Get the request query string strQuery = Request["q"].ToString(); //Create the XML-like string to be sent back to the request string strXmlNames = ""; //An arbitrary array of names that will be written to an XML Document. string[] arrStrNames = new string[5]{"Ian Suttle", "John Doe", "Alex Wright", "Albert Einstein", "Sierra Tracy"}; //Loop through the names, creating a psuedo XML element for each foreach(string strName in arrStrNames) { //If the request matches the beginning of a name, then add it to the string // otherwise it shouldn't be a valid match if (strName.ToLower().Substring(0, strQuery.Length) == strQuery.ToLower()) strXmlNames += "" + strName + ""; } //Prepend and append the parent element strXmlNames = "" + strXmlNames + ""; using (StreamWriter sw = new StreamWriter("TestFile.txt")) { sw.writeLine(strXmlNames); sw.writeLine("-----------------"); } //Create an XmlDocument object to store the XML string that we created XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(strXmlNames); //Create a navigator object to use in the XSL transformation XPathNavigator xPathNav = xDoc.CreateNavigator(); //Create the XSLTransform object XslTransform xslt = new XslTransform(); xslt.Load(Server.MapPath("Names.xslt")); //Do the transformation and send the results out to the Response object's output stream xslt.Transform(xPathNav, null, Response.OutputStream); } } }