简体   繁体   中英

Deserialize this XML File and getting data

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.

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.

Made it work after some minor modifications to your classes

XmlRoot("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; }

    [XmlElement("gridCol")]
    public List<GridCol> GridCol { get; set; }
}

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

    [XmlText]
    public string Content { get; set; }
}

Usage

XmlSerializer serializer = new XmlSerializer(typeof(Root));
using (var reader = new StreamReader(xmlFilePath))
{
    var root = ((Root)serializer.Deserialize(reader));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM