Generating C# Classes from WebEx Request XML

Just recently I worked on a project that required me to consume some WebEx services, after the first look at some sample code I  decided not to go the route of building XML strings whenever there was a need to communicate with the WebEx servers. I wanted the ability to serialize and deserialize xml requests and responses, so here is what I did to accomplish this in C# and xsd.exe.

Sample WebEx xml request: Authenticate user

<?xml version="1.0" encoding="ISO-8859-1"?>
<serv:message xmlns:xsi=""
        <bodyContent xsi:type="java:com.webex.service.binding.user.AuthenticateUser">
                message will go here
  1. Copy the XML string to notepad and save as helper.xml
  2. At the developer command prompt type cd\ then press Enter\Return
  3. Type the path to helper.xml then press Enter\Return
  4. At the command prompt type xsd helper.xml then press Enter\Return [This will generate the helper.xsd file]
  5. Next type xsd helper.xsd /c /f then press Enter\Return  [Will generate C# classes based on the helper.xsd file]

Now lets see how we can manually load these generated classes then serialize them to XML for posting to the WebEx REST API Servers.

private void TestSerialization()
 var msgSecurityContext = new messageHeaderSecurityContext()
 email = "",
 partnerID = "webexpartner",
 password = "pass",
 siteName = "ian0830",
 webExID = "siteadmin"

var msgBodyContent = new messageBodyBodyContent() { samlResponse = "samlResponse message will go here" };

var header = new messageHeader() { securityContext = new messageHeaderSecurityContext[] { msgSecurityContext } };
var body = new messageBody() { bodyContent = new messageBodyBodyContent[] { msgBodyContent } };

var msg = new message() { Items = new object[] { header, body } };

var xmlns = new System.Xml.Serialization.XmlSerializerNamespaces();

xmlns.Add("serv", "");
xmlns.Add("xsi", "");

System.Xml.Serialization.XmlSerializer searializer = new System.Xml.Serialization.XmlSerializer(msg.GetType());

searializer.Serialize(Console.Out, msg, xmlns);

This produces an identical XML string as the one shown above.