This tutorial will show you how to add an attribute to an XML stream returned from a Windows Communication Foundation (WCF) Web Service. Some knowledge of WCF and XML is required; the code is in C#. Below is an XML sample of an attributed XML stream comprised of an array of dynamically placed arguments.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: | <info xmlns="http://myns.com"> <args> <arg type="firstname"> <value>William</value> </arg> <arg type="lastname"> <value>Campbell</value> </arg> </info> |
Unfortunately, there is *no way* to get this exact piece of XML is in the current version of WCF as attributes in XML streams are not supported using DataContract!
The solution to follow uses the 'KnownType' WCF attribute to piggyback the Microsoft 'i:type' attribute. Using the method described in this article, we can get close to the above (desired) XML as the output we get looks like this:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: | <info xmlns="http://myns.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <args> <arg i:type="firstname"> <value>William</value> </arg> <arg i:type="lastname"> <value>Campbell</value> </arg> </info> |
Notice the difference is the addition of namespace 'i' that changes 'type' to 'i:type'. If you can live with the 'i:' prefix, this solution is for you.
With that said, let's get under way with a sample definition of a WCF service that will use my technique.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: | using System; using System.ServiceModel; using System.ServiceModel.Web; [ServiceContract] [DataContractFormat] public interface IRPCService { [OperationContract] [WebGet(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/getuserxml?id={userID}")] info GetUserXML(String userID); } |
GetUserXML is a function declaration in a .svc file. It is a RESTful (REpresentational State Transfer) Web Service call which would be invoked like this.
1: | http://www.experts-exchange.com/RPCService.svc/getuserxml?id=williamcampbell |
The function returns an 'info' class as 'plain' serialized XML to the caller. Within the plain serialized XML will be the embedded attributes. Each attribute that will appear in the serialized XML must be declared as a KnownType and have its own class implementation. The code below shows a base class that defines two KnownTypes.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: | using System; using System.Runtime.Serialization; [DataContract(Name="arg", Namespace="http://myns.com")] [KnownType(typeof(FirstNameArg))] [KnownType(typeof(LastNameArg))] public class BaseArg { public BaseArg() { } [DataMember(Name="value")] public String Value { get;set; } } [DataContract(Name="firstname", Namespace="http://myns.com")] public class FirstNameArg : BaseArg { } [DataContract(Name="lastname", Namespace="http://myns.com")] public class LastNameArg : BaseArg { } |
The Args FirsNameArg and LastNameArg can now be used in the info class.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: | using System; using System.Collections.Generic; using System.Runtime.Serialization; [DataContract(Name = "info", Namespace = "http://myns.com")] public class info { public info() { Args = new List<BaseArg>(); } public void AddArg(BaseArg arg, String value) { arg.Value = value; Args.Add(arg); } [DataMember(Name = "args")] List<BaseArg> Args { get; set; } } |
The info class results in the outer tag:
1: 2: | <info> </info> |
info declares an array of BaseArg with a [DataMember] contract of "args" resulting in:
1: 2: 3: 4: | <info> <args> </args> </info> |
Each BaseArg that is added results in an <arg> tag with an i:type inside it. The code DataContract(Name="firstname") above results in:
1: 2: 3: 4: 5: 6: | <info> <args> <arg i:type="firstname"> </arg> </args> </info> |
The value passed to the BaseArg classes 'Value' function becomes the body of the arg:
1: 2: 3: 4: 5: 6: 7: | <info> <args> <arg i:type="firstname"> <value>William</value> </arg> </args> </info> |
And so on for other args...
A simplified version of the function body for GetUserXML could be seen here:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: | public info GetUserXML(String userID) { // lookup userID in the Database info exinfo = new info(); exinfo.AddArg(new FirstNameArg(), "William"); exinfo.AddArg(new LastNameArg(), "Campbell"); return exinfo; } |
Although not an ideal solution to the attribute problem, this tutorial will help you move forward if you are blocked by the need for dynamic XML attributes in WCF.