Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
Hi All,
I am playing around with RESTful web servcies in WCF. I am using the online template in visual studio called 'WCF REST Service Template 40(CS)'.
Anyway, all is good and my service calls return appropriate XML data (which is what I am looking to return)
At first, I quite liked how easy it was to return a class and it would automatically be translated into XML. A simple example would be return a string with a value representing an authentication token, the XML returned would be:
...easy as pie! But now, I am deciding what I really want to return is something like...
...So my ultimate question is, what is the best way to make this happen?
I though it would probably work if I had a custom class called 'token' and I override whatever function handles the 'to xml' part of it - but this would be overkill and just doesn't seem right
What I would really like to be able to do, is to include a 'View' as would be possible using the ASP.Net MVC framework. So I could define my XML format in a View as easy as (in MVC 3)...
</token> this possible in WCF? Perhaps I should use MVC 3 to do the RESTful service instead of WCF? Any thoughts?
Posted 4-Apr-11 0:10am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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();
    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());
    return doc;
These two above functions allow for an xml file containing:
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);//params here are View and Model
the returned XML response would be...
...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
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 719
1 CPallini 245
2 CHill60 235
3 Sergey Alexandrovich Kryukov 230
4 TheRealSteveJudge 208
0 Sergey Alexandrovich Kryukov 9,078
1 OriginalGriff 6,771
2 Peter Leow 4,262
3 Zoltán Zörgő 3,809
4 Richard MacCutchan 2,663

Advertise | Privacy | Mobile
Web03 | 2.8.150128.1 | Last Updated 5 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100