using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml; using System.Collections; public partial class _Default : System.Web.UI.Page { //private string pollid = ""; //id for the poll //private string title = ""; //title of the poll //private string IP = ""; //IP of the submitter //private int timestamp = 0; //when submitted //private Hashtable questiontypes; //associates questions w/ the typ they are (radio, text, etc) //Hashtable results; //holds hashtable representation of poll results //Hashtable confighash; //holds CMS configuration private XmlDocument resultsDoc; private XmlDocument formDoc; private XmlDocument formattedDoc; private XmlDocument configDoc; private string pollid; private string polltitle; protected void Page_Load(object sender, EventArgs e) { formDoc = new XmlDocument();//Holds the HTML pulled from the survey page (the actual HTML used to make the form) resultsDoc = new XmlDocument(); //Holds the XML from surveyresults.xml formattedDoc = new XmlDocument(); //Holds XML for survey-formatted-results (the end result) configDoc = new XmlDocument(); //Holds XML for survey-config.xml polltitle = ""; pollid = ""; //Only works if surveyresults.xml exists if (System.IO.File.Exists("surveyresults.xml")) { resultsDoc.Load("surveyresults.xml"); string p; //Only need the poll name form the config file for now if (System.IO.File.Exists("survey-config.xml")) { configDoc.Load("survey-config.xml"); p = configDoc.SelectSingleNode("//poll").ChildNodes[0].Value; } else { main.Text += "Could not find config file"; return; } //Load HTML for survey page into a string System.IO.StreamReader sr = new System.IO.StreamReader(p); string formstr = sr.ReadToEnd(); //Grab form HTML from survey page and load into doc (HTML between
) int start = formstr.IndexOf("") + "".Length; formDoc.LoadXml(formstr.Substring(start, formstr.IndexOf("") - start)); //First tally results, then add to XML doc buildOutput(buildResults()); outputToCMS(); } } //Builds a tally for how many times an answer was given to a question (multiple choice only) private Hashtable buildResults() { Hashtable results = new Hashtable(); foreach (XmlNode n in resultsDoc.SelectSingleNode("//survey").ChildNodes) { //If the poll id of the node in surveyresults.xml matches the id of the poll pulled from the config file if (n.Attributes["id"].Value == formDoc.SelectSingleNode("//input[@name = 'id']").Attributes["value"].Value) { pollid = n.Attributes["id"].Value; polltitle = n.Attributes["title"].Value; //For each respone's multiple choice node foreach (XmlNode r in n) { XmlNode m = r.SelectSingleNode("multiplechoice"); //If question/answer nodes not made, make them and set tally to 1. Else inc tally foreach (XmlNode q in m.ChildNodes) { if (results.ContainsKey(q.Name)) { Hashtable tempq = ((Hashtable)results[q.Name]); if (tempq.ContainsKey(q.ChildNodes[0].Value)) { tempq[q.ChildNodes[0].Value] = Convert.ToInt32(tempq[q.ChildNodes[0].Value]) + 1; } else { tempq[q.ChildNodes[0].Value] = "1"; } } else { results[q.Name] = new Hashtable(); ((Hashtable)results[q.Name])[q.ChildNodes[0].Value] = 1; } } } break; } } return results; } //Takes Hashtable from buildResults and converts it to XML private void buildOutput(Hashtable results) { XmlNode q; XmlNode a; Hashtable qtable; //Create root tag with poll id and title attributes XmlElement root = formattedDoc.CreateElement("results"); XmlAttribute i = formattedDoc.CreateAttribute("id"); XmlAttribute t = formattedDoc.CreateAttribute("title"); i.Value = pollid; t.Value = polltitle; root.Attributes.Append(i); root.Attributes.Append(t); //Loop through the questions foreach (string qkey in results.Keys) { try { q = formattedDoc.SelectSingleNode(qkey); } catch { main.Text += "In function buildOutput: select node based on 'qkey' failed."; return; } if (q == null) { q = formattedDoc.CreateElement(qkey); } qtable = ((Hashtable)results[qkey]); //Loop through the answers and build XML nodes with the tally for the value foreach (string akey in qtable.Keys) { string akey2 = "_" + akey.Replace(" ", "-"); a = formattedDoc.CreateElement(akey2); a.AppendChild(formattedDoc.CreateTextNode(qtable[akey].ToString())); //Append answer to question q.AppendChild(a); } //Append question to results root.AppendChild(q); } //append results to the doc formattedDoc.AppendChild(root); } private void outputToCMS() { string CMSURL; //URL of the CMS string cmsdirectory; //where in CMS to write to string username; //for login to CMS string password; //for login to CMS string data; //data to submit //Load up other config parameters configDoc.Load("survey-config.xml"); CMSURL = configDoc.SelectSingleNode("//CMSURL").ChildNodes[0].Value; cmsdirectory = configDoc.SelectSingleNode("//cmsdirectory").ChildNodes[0].Value; username = configDoc.SelectSingleNode("//username").ChildNodes[0].Value; password = configDoc.SelectSingleNode("//password").ChildNodes[0].Value; //Convert XML to string System.IO.StringWriter sw = new System.IO.StringWriter(); System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw); formattedDoc.WriteTo(xw); data = sw.ToString(); //Build WSDL object for SOAP call wsdl.AssetOperationHandlerService service = new wsdl.AssetOperationHandlerService(); service.Url = CMSURL + "/ws/services/AssetOperationService?wsdl"; wsdl.authentication auth = new wsdl.authentication(); auth.username = username; auth.password = password; wsdl.xmlBlock block = new wsdl.xmlBlock(); block.name = "survey-formatted-results"; block.parentFolderPath = cmsdirectory; block.path = ""; block.metadataSetPath = "/Default"; block.xml = data; wsdl.asset asset = new wsdl.asset(); asset.Item = block; wsdl.operationResult result = new wsdl.operationResult(); //Attempt an edit. If that fails then create. try { result = service.edit(auth, asset); if (result.success == "false") { block.path = ""; result = service.create(auth, asset); } } catch { main.Text += "Block creation/edit request failed to conform to the WSDL.\n" + result.message; } } }