Thursday, May 19, 2005

Creating a Class from xml

For any of you writing api's for today's hottest websites (say that like your a game show host), there is a great tool that will turn an xml document into a cs file. The tool is xsd and comes with Visual Studio .NET. Here's how it works:

1. Use the following text to make a file called c:\table.xml:

<?xml version="1.0" encoding="UTF-8"?>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
<tr>
<td>col 1 row 1</td>
<td>col 2 row 1</td>
<td>col 3 row 1</td>
</tr>
<tr>
<td>col 1 row 2</td>
<td>col 2 row 2</td>
<td>col 3 row 2</td>
</tr>
<tr>
<td>col 1 row 3</td>
<td>col 2 row 3</td>
<td>col 3 row 3</td>
</tr>
</table>

2. Open cmd and goto c:.
3. Run the following: xsd table.xml
4. See the file created c:\table.xsd
5. Run the following: xsd table.xsd /t:lib /l:cs /c /namespace:xmltest
6. See the file created c:\table.cs

Isn't that cool? It makes all the classes and even makes arrays. Admittedly, it would be better if it made strongly typed collections, but it is not bad.


[tags: ]

0 Comments:

Post a Comment

<< Home