How To: Automatic parsing of an XML document with C#

Ok, this is something I learnt today. I have a web service that responses me a string with a xml content that is something like this:

1. Getting a complete XML example

<indicadores xmlns=”“>
<strnombre>DTF 90 Días (E.A.)</strnombre>

I saved this XML code to a file into my projects folder c:\inetpub\wwwroot\CoreSimulacionWS\Indicadores.xml, then I added the file to the project.

2. Creating XML Schema

Open the XML file you just added to the project, right click the content and click on the “Generate Schema” option. A new file called Indicadores.xsd is created with a schema that describes the content in the XML file.

<xs:schema id=”Indicadores” targetnamespace=”; mstns=”; xmlns=”; xs=”; msdata=”urn:schemas-microsoft-com:xml-msdata” attributeformdefault=”qualified” elementformdefault=”qualified”>
<xs:element name=”Indicadores” isdataset=”true” locale=”es-ES” enforceconstraints=”False”>
<xs:choice maxoccurs=”unbounded”>
<xs:element name=”Indicador”>
<xs:element name=”strNombre” type=”xs:string” minoccurs=”0″>
<xs:element name=”strValorIndicador” type=”xs:string” minoccurs=”0″>
<xs:element name=”datFechaInicioVigencia” type=”xs:string” minoccurs=”0″>
<xs:element name=”numVariacion” type=”xs:string” minoccurs=”0″>
<xs:element name=”numId” type=”xs:string” minoccurs=”0″>

3. Generating the Classes from the XML Schema

With the VS.NET IDE there is a xsd.exe executable bundled with it. This file is used to either generate a XML Schema file from a class definition or to create the set of classes that matches a defined XML Schema. In this case we are going to use it to create the set of classes from the XML Schema that we already got in the last step.

Then we add the IDE’s bin directory to the system path (C:\Archivos de
programa\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin), and then open the windows command line tool. Once into the console let’s get to the project’s path c:\inetpub\wwwroot\CoreSimulacionWS\) and once there:

C:\Inetpub\wwwroot\CoreSimulacionWS>xsd Indicadores.xsd /classes

As a result we’ll have a new Indicadores.cs file that we got to add to our project. We should also modify this class file to set he proper namespace for the classes.

namespace CoreSimulacionWS {
[System.Xml.Serialization.XmlRootAttribute(Namespace=”;, IsNullable=false)]
public class Indicadores {
public IndicadoresIndicador[] Items;
public class IndicadoresIndicador {
public string strNombre;
public string strValorIndicador;
public string datFechaInicioVigencia;
public string numVariacion;
public string numId;

4. Getting the job done

Finally we have to order a serializer to deserialize our Indicadores.xml file into the set of classes created that match the XML Schema:

FileStream stream = new FileStream(“c:\\inetpub\\wwwroot\\CoreSimulacionWS\\Indicadores.xml”, FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(Indicadores));
Indicadores indicadoresParsedFile = (Indicadores)serializer.Deserialize(stream);

Now we have an object of class Indicadores that groups all the inner nodes of the XML document into IndicadoresIndicador class objects.

Have fun!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s