C# code to dynamically generate CoT XML

I've been working on implementing CoT XML standard for describing location based data.  Here is the C# code for an.aspx page that will render XML

More infomation about CoT can be found here:

using System;using System.Data;using System.Configuration;using System.Collections;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;public partial class cot: System.Web.UI.Page { GPSXML gpsEntity; string xmlFile = "http://timhibbard.com/wherestim/gpsdata.xml"; protected void Page_Load(object sender, EventArgs e) { try { //pull requested xml file out of query string string tmpXml = Request.QueryString["xml"]; if (!string.IsNullOrEmpty(tmpXml)) { xmlFile = tmpXml; } gpsEntity = new GPSXML(xmlFile); } catch (Exception) {

} BuildCoT;

} private void BuildCoT { Response.Clear; Response.ContentType = "text/xml"; Response.ContentEncoding = System.Text.Encoding.UTF8;

GPSXML.GPSDataObject gps = gpsEntity.GetGPSData; System.IO.MemoryStream stream = new System.IO.MemoryStream; XmlTextWriter xmlWriter = new XmlTextWriter(stream, System.Text.Encoding.UTF8);

xmlWriter.WriteStartDocument; xmlWriter.WriteWhitespace(Environment.NewLine);

xmlWriter.WriteStartElement("event"); xmlWriter.WriteAttributeString("version", "2.0"); //my unique identifier xmlWriter.WriteAttributeString("uid", "engraph.wherestim." + gpsEntity.Name.Replace(" ","_")); //type description for where's tim atom-friendly-ground xmlWriter.WriteAttributeString("type", "a-f-G"); //this data is being produced right now xmlWriter.WriteAttributeString("time", DateTime.Now.ToUniversalTime.ToString("s") + "Z"); //the time scope of this data starts right now xmlWriter.WriteAttributeString("start", DateTime.Now.ToUniversalTime.ToString("s") + "Z"); //this data expires in two minutes xmlWriter.WriteAttributeString("stale", DateTime.Now.AddSeconds(120).ToUniversalTime.ToString("s") + "Z"); //this data is machine-generated xmlWriter.WriteAttributeString("how", "m-g");

xmlWriter.WriteStartElement("point"); //my location xmlWriter.WriteAttributeString("lat", gps.Lat.ToString); xmlWriter.WriteAttributeString("lon", gps.Lon.ToString); //meters of potential deviation xmlWriter.WriteAttributeString("ce", "100"); //height above location xmlWriter.WriteAttributeString("hae", "0"); xmlWriter.WriteAttributeString("le", "100"); xmlWriter.WriteEndElement;

xmlWriter.WriteStartElement("detail"); xmlWriter.WriteStartElement("track"); //the direction i am heading xmlWriter.WriteAttributeString("course", gps.Heading.ToString); //speed in meters per second xmlWriter.WriteAttributeString("speed", (double.Parse(gps.Speed.ToString) * 0.44704).ToString); xmlWriter.WriteEndElement; xmlWriter.WriteEndElement;

xmlWriter.WriteWhitespace(Environment.NewLine); xmlWriter.WriteEndDocument; xmlWriter.Flush;

System.IO.StreamReader reader; stream.Position = 0;

reader = new System.IO.StreamReader(stream); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd); //write to response stream Response.BinaryWrite(bytes);

Response.End; } }

Technorati tags:,,,,

This article is part of the GWB Archives. Original Author: Tim Hibbard

New on Geeks with Blogs