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=”http://tempuri.org/Indicadores1.xsd“>
<indicador>
<strnombre>UVR</strnombre>
<strvalorindicador>145.2891</strvalorindicador>
<datfechainiciovigencia>20041007</datfechainiciovigencia>
<numvariacion>1</numvariacion>
<numid>83</numid>
</indicador>
<indicador>
<strnombre>I-TES</strnombre>
<strvalorindicador>104.68</strvalorindicador>
<datfechainiciovigencia>20041005</datfechainiciovigencia>
<numvariacion>1</numvariacion>
<numid>178</numid>
</indicador>
<indicador>
<strnombre>DTF 90 Días (E.A.)</strnombre>
<strvalorindicador>7.66%</strvalorindicador>
<datfechainiciovigencia>20041004</datfechainiciovigencia>
<numvariacion>-1</numvariacion>
<numid>32</numid>
</indicador>
</indicadores>

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=”http://tempuri.org/Indicadores1.xsd&#8221; mstns=”http://tempuri.org/Indicadores1.xsd&#8221; xmlns=”http://tempuri.org/Indicadores1.xsd&#8221; xs=”http://www.w3.org/2001/XMLSchema&#8221; msdata=”urn:schemas-microsoft-com:xml-msdata” attributeformdefault=”qualified” elementformdefault=”qualified”>
<xs:element name=”Indicadores” isdataset=”true” locale=”es-ES” enforceconstraints=”False”>
<xs:complextype>
<xs:choice maxoccurs=”unbounded”>
<xs:element name=”Indicador”>
<xs:complextype>
<xs:sequence>
<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″>
</xs:element>
</xs:element>
</xs:element>
</xs:element>
</xs:element>
</xs:sequence>
</xs:complextype>

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.XmlTypeAttribute(Namespace=”http://tempuri.org/Indicadores1.xsd&#8221;)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”http://tempuri.org/Indicadores1.xsd&#8221;, IsNullable=false)]
public class Indicadores {
[System.Xml.Serialization.XmlElementAttribute(“Indicador”)]
public IndicadoresIndicador[] Items;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace=”http://tempuri.org/Indicadores1.xsd&#8221;)]
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!

Advertisement

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s