Vishal Suthar
Vishal Suthar

Reputation: 17194

Deserialize nested level XML

I am trying to retrieve data from Zoho API and then I want to Deserialize a returned data in an object.

The XML structure:

<response uri="/crm/private/xml/Potentials/getSearchRecords">
    <result>
        <Potentials>
            <row no="1">
                <FL val="POTENTIALID">0000000000000</FL>
                <FL val="SMOWNERID">000000000000</FL>
                .......
            </row>
            <row no="2">
                .......
            </row>
        </Potentials>
    </result>
</response>

And here what I tried but didn't succeed.

[Serializable()]
public class ZohoXML
{
    public ZohoXML() { }

    [XmlElement("POTENTIALID")]
    public string POTENTIALID { get; set; }
    ........
}

[XmlRoot("response")]
public class Response
{
    [XmlElement("result")]
    public Result[] ResultList { get; set; }
}

public class Result
{
    [XmlArray("Potentials"), XmlArrayItem("row")]
    public Row[] RowList { get; set; }
}

[XmlRoot("row")]
public class Row
{
    [XmlArray("Potentials")]
    [XmlArrayItem("row", typeof(ZohoXML))]
    public ZohoXML[] ZohoXML { get; set; }
}

And this is how I deserialize:

Response ZohoXMLCollection = null;

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Response));

StreamReader reader = new StreamReader(strFile);
ZohoXMLCollection = (Response)serializer.Deserialize(reader);
reader.Close();

Can anybody please help on identifying an issue in the code ?

Upvotes: 1

Views: 2199

Answers (1)

DGibbs
DGibbs

Reputation: 14618

If you are using a newer version of VS you can use Edit > Paste Special > Paste XML as Classes in the edit menu. Assuming your XML structure is correct this will give you:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class response
{

    private responseResult resultField;

    private string uriField;

    /// <remarks/>
    public responseResult result
    {
        get
        {
            return this.resultField;
        }
        set
        {
            this.resultField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string uri
    {
        get
        {
            return this.uriField;
        }
        set
        {
            this.uriField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class responseResult
{

    private responseResultRow[] potentialsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("row", IsNullable = false)]
    public responseResultRow[] Potentials
    {
        get
        {
            return this.potentialsField;
        }
        set
        {
            this.potentialsField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class responseResultRow
{

    private responseResultRowFL[] flField;

    private byte noField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("FL")]
    public responseResultRowFL[] FL
    {
        get
        {
            return this.flField;
        }
        set
        {
            this.flField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte no
    {
        get
        {
            return this.noField;
        }
        set
        {
            this.noField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class responseResultRowFL
{

    private string valField;

    private byte valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string val
    {
        get
        {
            return this.valField;
        }
        set
        {
            this.valField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public byte Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}

You will want to clean up the names but for now this is fine.

Then your de-serialization will look like this:

XmlSerializer serializer = new XmlSerializer(typeof(response));
response res = new response();
using (StreamReader reader = new StreamReader(fileLocation))
{
      res = serializer.Deserialize(reader) as response;
}

Note that StreamReader implements the IDisposable interface so should be used within a using block to ensure automatic disposable of resources.

Upvotes: 2

Related Questions