How to create OOP representations of XML

If you have an XML file and you are seeking for the best way to deal with it inside your code. You do not want to use XPath Queries intensively in your code and want to have the best way to deal with that XML… I faced such a case where I need to build a special configuration file for an FTP windows service. The way I wanted to build this XML file was in a hierarchy way something looks like the following XML:

<FtpConnection Code="CodeName" Direction="Upload">

<Ftp>

<RemoteHost>IP or the DNS name for the remote FTP server</RemoteHost>

<Username>FTP USerName</Username>

<Password>password</Password>

<ConnectionTimeOut>50</ConnectionTimeOut>

</Ftp>

<Security>

<IsSecured>true</IsSecured>

<ScretPhase>Sec Phrase Goes here</ScretPhase>

</Security>

</FtpConnection>

 

This FTP service suppose to work with more than on FTPConnection. Thus the above configuration snap might occur more than one time. Which make it is harder to deal with .

The best way to deal with this configuration inside code is to create a object oriented representation of it and load this XML into it. then let your code deal with the OOP classes instead of directly with XML.

Well, there is a very easy way to build a OOP presentation of this XML. There is a tool that is shipped with .Net framework & visual studio called XSD. You can use this tool to generate the OOP representation by first create XSD schema for your XML then convert that schema to OOP presentations. Below is how to do that step by step.

  1. I created a file on my desktop and I called it "FTPConfig.xml", I paste the following XML into it.

<Root>

<FtpConnection Code="CodeName" Direction="Upload">

<Ftp>

<RemoteHost>IP or the DNS name for the remote FTP server</RemoteHost>

<Username>FTP USerName</Username>

<Password>password</Password>

<ConnectionTimeOut>50</ConnectionTimeOut>

</Ftp>

<Security>

<IsSecured>true</IsSecured>

<ScretPhase>Sec Phrase Goes here</ScretPhase>

</Security>

</FtpConnection>

<FtpConnection Code="Code2Name" Direction="Upload">

<Ftp>

<RemoteHost>IP or the DNS name for the remote FTP server</RemoteHost>

<Username>FTP USerName</Username>

<Password>password</Password>

<ConnectionTimeOut>50</ConnectionTimeOut>

</Ftp>

<Security>

<IsSecured>true</IsSecured>

<ScretPhase>Sec Phrase Goes here</ScretPhase>

</Security>

</FtpConnection>

</Root>

· Note that I duplicated the FTPConnection tag twice, I did that for a purpose. I did that to tell XSD tool to expect this tag to occur more than once. Thus when XSD tool build the Schema representation of it, it will make the MAX occurrence of this tag as infinite

     2. From Start menu, open "Visual Studio 2008 Command Prompt"… it is Command prompt but has special configurations to open some Command Prompt tools shipped with VS 2008

      3. We need to set the current folder on CMD to be the desktop. To do so, write the following comand

Cd c:\users\[windowsUserName]\desktop

· Where [WindowsUserName] is the login name that you are using to login into your PC

       4. Now we need to create the XSD schema file for this XML. To do that, write the following command into your CMD

xsd FTPConfig.xml

· The output from this command is an XSD file and it will placed on your Desktop; the name of this XSD file suppose to be FTPConfig.xsd; My advice to you is to take couple of minutes to review this Schema file and make sure it is meeting your expectation.

       5. Now we need to Create the OOP presentation of this XSD; to do so, write the following command

xsd FTPConfig.xsd /classes /language:CS

a. The meaning of parameter /Classes : will make the output of this command to be Classes not DataSet.

b. While the Meaning of parameter /language is the programming language in which the code will be generated; CS means C# language

    The output form this command suppose a C# code placed on your desktop.

  •  My recommendation to you is to keep all the generated classes into a single class and do not split each class into a separated file. I am saying this because you might decide to change the configuration a little bit and you need to regenerate those classes again. So if you split those classes into different files, then you will get into a loop of re-splitting the classes again and again.

I hope you will find this topic helpful. Please feel free to send any comments/questions.