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="http://www.w3.org/2001/XMLSchema-instance"
xmlns:serv="http://www.webex.com/schemas/2002/06/service">
    <header>
    <securityContext>
        <siteName>ian0830</siteName>
        <webExID>siteadmin
        <password>pass</password>
        <partnerID>webexpartner
        <email>johnsmith@xyz.com</email>
    </securityContext>
    </header>
    <body>
        <bodyContent xsi:type="java:com.webex.service.binding.user.AuthenticateUser">
            <samlResponse>
                message will go here
            </samlResponse>
       </bodyContent>
    </body>
</serv:message>
  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 = "johnsmith@xyz.com",
 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", "http://www.webex.com/schemas/2002/06/service");
xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

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

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

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

Advertisements

Form Showing As Class In Visual Studio 2010 Solution Explorer

Sometimes while working on WinForms applications a very strange thing happens, when I add a new form to the project it shows up as a C# class. This is very annoying because I can not readily get to the design view for that form. Luckily there is a simple fix.

1. Unload the project.
2. Search for the specific form in the xml file.
3. Replace xml with following.

<Compile Include="[formname].cs">
    <SubType>Form</SubType>
</Compile>

4. Save and reload the project.