Prabulg
Prabulg

Reputation: 53

No data when deserializing XML to Object in C#

Hello i am trying to deserialize a XML file into object but i get no values.I think there is a problem with the class models. When I run it i get no data populated in the resulting object.

 XDocument test = XDocument.Load(UrlAdrress);
 var result = XMLHelper.FromXML<XmlSportsModel>(test);

The structure is correct enter image description here

Here is my xml looks like and i cant change it. XMLFile

My deserializer

 public static class XMLHelper
{
    public static T FromXML<T>(XDocument data)
    {
        var xmlSerializer = new XmlSerializer(typeof(T));
        using (var reader = data.CreateReader())
        {
            return (T)xmlSerializer.Deserialize(reader);
        }               
    }
}

My Models

[XmlRoot("XmlSports")]
public class XmlSportsModel
{  
    [XmlElement("Sport")]
    public SportModel[] Sports { get; set; }       
}

    public class SportModel
{
    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }

    [XmlElement(ElementName = "ID")]
    public string Id { get; set; }
    [XmlElement(ElementName = "Event")]
    public EventModel[] Events { get; set; }
}


   public class EventModel
{
    [XmlElement("Name")]
    public string Name { get; set; }
    [XmlElement("ID")]
    public int Id { get; set; }
    [XmlElement("CategoryID")]
    public int CategoryId { get; set; }
    [XmlElement("IsLive")]
    public bool IsLive { get; set; }
    [XmlElement("Match")]
    public MatchModel[] Matches { get; set; }
}

 public class MatchModel
{
    [XmlElement("Name")]
    public string Name { get; set; }
    [XmlElement("ID")]
    public int Id { get; set; }
    [XmlElement("StartDate")]
    public DateTime StartDate { get; set; }
    [XmlElement("MatchType")]
    public MatchType Matchtype { get; set; }
    [XmlElement("Bet")]
    public BetModel[] Bets { get; set; }
}

public enum MatchType
{
    PreMatch,
    Live
}

Upvotes: 0

Views: 1183

Answers (1)

Siderite Zackwehdex
Siderite Zackwehdex

Reputation: 6570

In Visual Studio 2012 and later you can take the XML and go to Edit -> Paste Special -> Paste XML as classes. If you don't have it, there are tools online that do that for you.

Just to help with the transformation, here is the result from the XML pasting:

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

    private XmlSportsSport[] sportField;

    private System.DateTime createDateField;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public System.DateTime CreateDate
    {
        get
        {
            return this.createDateField;
        }
        set
        {
            this.createDateField = value;
        }
    }
}

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

    private XmlSportsSportEvent[] eventField;

    private string nameField;

    private ushort idField;

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

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

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ushort ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }
}

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

    private XmlSportsSportEventMatch[] matchField;

    private string nameField;

    private ushort idField;

    private ushort categoryIDField;

    private bool isLiveField;

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

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

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ushort ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ushort CategoryID
    {
        get
        {
            return this.categoryIDField;
        }
        set
        {
            this.categoryIDField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool IsLive
    {
        get
        {
            return this.isLiveField;
        }
        set
        {
            this.isLiveField = value;
        }
    }
}

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

    private XmlSportsSportEventMatchBet[] betField;

    private string nameField;

    private uint idField;

    private System.DateTime startDateField;

    private string matchTypeField;

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

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

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public uint ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public System.DateTime StartDate
    {
        get
        {
            return this.startDateField;
        }
        set
        {
            this.startDateField = value;
        }
    }

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

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

    private XmlSportsSportEventMatchBetOdd[] oddField;

    private string nameField;

    private uint idField;

    private bool isLiveField;

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

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

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public uint ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool IsLive
    {
        get
        {
            return this.isLiveField;
        }
        set
        {
            this.isLiveField = value;
        }
    }
}

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

    private string nameField;

    private uint idField;

    private decimal valueField;

    private decimal specialBetValueField;

    private bool specialBetValueFieldSpecified;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public uint ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

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

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal SpecialBetValue
    {
        get
        {
            return this.specialBetValueField;
        }
        set
        {
            this.specialBetValueField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool SpecialBetValueSpecified
    {
        get
        {
            return this.specialBetValueFieldSpecified;
        }
        set
        {
            this.specialBetValueFieldSpecified = value;
        }
    }
}

And the code used was just:

XDocument test = XDocument.Load("sportxml.xml");
var result = XMLHelper.FromXML<XmlSports>(test);

Upvotes: 1

Related Questions