Well I have been looking into this and what I have found so far is as follows.
In order to return XML formatted exactly how I wanted it, I found
this post[
^] which shows how to convert an XMLDocument into a Message. Then with a further helper class my calling code can now be as simple as this...
[WebGet(UriTemplate = "Test")]
public Message Test()
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml("<token>1234</token>");
return CustomMessage.XML(doc);
}
...which produces the XML result I was looking for. I was also able to produce an MVC style view parser using a bit of Regex and Reflection to parse for variables in the XML file and substitute them for matching parameters in a given class instance...
public static Message XML(string view, object model)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(System.Web.Hosting.HostingEnvironment.MapPath("~/Views/" + view + ".xml"));
return new CustomResponses().CreateResponseMessage(doc, model);
}
public System.Xml.XmlDocument MergeDoc(System.Xml.XmlDocument doc, object model)
{
string xml = doc.OuterXml;
System.Reflection.PropertyInfo[] properties = model.GetType().GetProperties();
foreach (Match match in Regex.Matches(xml, "@ViewBag.[a-zA-Z0-9]{1,}", RegexOptions.None))
{
string propertyName = match.Value.Replace("@ViewBag.", "");
System.Reflection.PropertyInfo property = model.GetType().GetProperty(propertyName);
xml = xml.Replace(match.Value, property.GetValue(model, null).ToString());
}
doc.LoadXml(xml);
return doc;
}
These two above functions allow for an xml file containing:
<token>@ViewBag.Token</token>
to be merged with a class instance and used like:
public class TokenClass
{
public string Token { get; set; }
}
[WebGet(UriTemplate = "Test2")]
public Message Test2()
{
TokenClass token = new TokenClass() { Token = "123456" };
return CustomMessage.XML("Token", token);
}
the returned XML response would be...
<token>123456</token>
...this is not 100% ideal as you do not have the extra functionality of parsing code within the xml file (such as a for loop) that would be possible with the Razor view engine used in MVC 3 (there is a
post here[
^] that shows how the Razor engine can be used outside of MVC 3 that may interest some but I am not sure it is what I want so I may chose to leave it out)
Anyway, I will probably continue to investigate further possibilities but hopefully this information will be of use to someone else. Also, if anyone reading this has any further comments, I would be interested in hearing them
EDIT:
Here[
^] is a nice little article on how to use the Razor view engine without MVC and I can say that it works pretty well