Deserialize this XML File and getting data

staff614

I have this XML File:

<readyGrid>
<gridRows>
<gridRow id="0">
<gridCol id="1">N.Doc.</gridCol>
<gridCol id="2">N.Doc. solo numero</gridCol>
<gridCol id="3">Data</gridCol>
<gridCol id="4">Intestatario</gridCol>
<gridCol id="5">Causale</gridCol>
<gridCol id="6">Stato</gridCol>
<gridCol id="7">Tot.doc.</gridCol>
<gridCol id="8">Data</gridCol>
<gridCol id="9">Peso</gridCol>
<gridCol id="10">Contrassegno</gridCol>
<gridCol id="11">Porto</gridCol>
<gridCol id="12">CAP</gridCol>
<gridCol id="13">Destinazione</gridCol>
<gridCol id="14">Indirizzo</gridCol>
<gridCol id="15">Citta'</gridCol>
<gridCol id="16">Provincia</gridCol>
<gridCol id="17">Colli</gridCol>
<gridCol id="18">Email</gridCol>
<gridCol id="19">Servizio</gridCol>
<gridCol id="20">Pagamento</gridCol>
<gridCol id="21">Fermo deposito</gridCol>
<gridCol id="22">Telefono destinatario</gridCol>
<gridCol id="23">Cellulare intestatario</gridCol>
</gridRow>
<gridRow id="1">
<gridCol id="1">any</gridCol>
<gridCol id="2">any</gridCol>
<gridCol id="3">any</gridCol>
<gridCol id="4">any</gridCol>
<gridCol id="5">any</gridCol>
<gridCol id="6">any</gridCol>
<gridCol id="7">any</gridCol>
<gridCol id="8">any</gridCol>
<gridCol id="9">any</gridCol>
<gridCol id="10">any</gridCol>
<gridCol id="11">any</gridCol>
<gridCol id="12">any</gridCol>
<gridCol id="13">any</gridCol>
<gridCol id="14">any</gridCol>
<gridCol id="15">any</gridCol>
<gridCol id="16">any</gridCol>
<gridCol id="18">any</gridCol>
<gridCol id="19">any</gridCol>
<gridCol id="20">any</gridCol>
<gridCol id="22">any</gridCol>
</gridRow>
</gridRows>
</readyGrid>

It is a dynamic file where there can be many gridRow, all inside the tag .

: where the id is 0, it says the header column, and from id="1" it list all the data.

I need to deserialize this .XML file in classes. Here is what I've got at the moment:

[XmlRoot(Namespace = "", ElementName = "readyGrid")]
public class Root
{
    [XmlElement("gridRows")]
    public List<GridRows> GridRows { get; set; }
}

public class GridRows
{
    [XmlElement("gridRow")]
    public List <GridRow> GridRow { get; set; }
}

public class GridRow
{
    [XmlAttribute("id")]
    public int Id { get; set; } 
    [XmlAttribute("gridCol")]
    public List<GridCol> GridCol { get; set; }
}

public class GridCol
{
    [XmlAttribute("id")]
    public int idColumn { get; set; }
    public string Content { get; set; }
}

And the code that should deserialize and put in classes:

XmlSerializer serializer = new XmlSerializer(typeof(Root));
var reader = new StreamReader(uploadFilePath);
List<GridRows> results = ((Root)serializer.Deserialize(reader)).GridRows;

However last code sample doesn't work, crashing on the first line. (System.InvalidOperationException, error in reflection .. )

I wonder if there's an easiest way to deserialize this XML file in classes or what I've to change in the code in order to make it work. Important thing is that I take the id="" and the content between tag.

Georg

The easiest way is certainly still the xsd.exe that comes with the .NET SDK. On a developer command prompt, you can call it like

xsd <Path to your example XML file> /classes

It will generate classes for you that resemble the structure of your XML file. As an alternative, it can also generate a schema for you and there are several option for you to tune the code generation, for example by providing the namespace. Just call xsd.exe without any arguments and it will give you instructions how to use it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related