Click here to Skip to main content
15,443,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i am having trouble deserializing Xml. I get AccountInformation to work but it wont work with the Leauges. The Xml doesnt contain any tag for "Leauges" and i dont want to add that tag to get it to work. Is there any other way to "fix" it?


[Serializable()]
[XmlRoot(ElementName = "XMLSOCCER.COM", Namespace = "")]
public class XMLSOCCER
{
    [XmlArray("Leauge")]
    [XmlArrayItem("Leauge", typeof(Leauge))]
    public Leauge[] Leauges { get; set; }

    [XmlElement(ElementName = "AccountInformation")]
    public string AccountInformation { get; set; }

}


[Serializable()]
public class Leauge
{
    [XmlElement(ElementName = "Id")]
    public int Id { get; set; }

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

    [XmlElement(ElementName = "Country")]
    public string Country { get; set; }

    [XmlElement(ElementName = "Historical_Data")]
    public string Historical_Data { get; set; }

    [XmlElement(ElementName = "Fixtures")]
    public string Fixtures { get; set; }

    [XmlElement(ElementName = "LiveScore")]
    public string Livescore { get; set; }

    [XmlElement(ElementName = "NumberOfMatches")]
    public int NumberOfMatches { get; set; }

    [XmlElement(ElementName = "LatestMatch")]
    public DateTime LatestMatch { get; set; }
}



XML:

XML
<XMLSOCCER.COM>
  <League>
    <Id>1</Id>
    <Name>English Premier League</Name>
    <Country>England</Country>
    <Historical_Data>Yes</Historical_Data>
    <Fixtures>Yes</Fixtures>
    <Livescore>Yes</Livescore>
    <NumberOfMatches>5700</NumberOfMatches>
    <LatestMatch>2015-05-24T16:00:00+00:00</LatestMatch>
  </League>
  <League>
    <Id>2</Id>
    <Name>English League Championship</Name>
    <Country>England</Country>
    <Historical_Data>Yes</Historical_Data>
    <Fixtures>Yes</Fixtures>
    <Livescore>Yes</Livescore>
    <NumberOfMatches>8372</NumberOfMatches>
    <LatestMatch>2015-05-02T13:15:00+00:00</LatestMatch>
  </League>
  <League>
    <Id>5</Id>
    <Name>Serie A</Name>
    <Country>Italy</Country>
    <Historical_Data>Yes</Historical_Data>
    <Fixtures>Yes</Fixtures>
    <Livescore>Yes</Livescore>
    <NumberOfMatches>5404</NumberOfMatches>
    <LatestMatch>2015-05-31T20:45:00+00:00</LatestMatch>
  </League>
  <AccountInformation>
   Confidential info
  </AccountInformation>
</XMLSOCCER.COM>


Deserialize code:

C#
public static void Main(string[] args)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(XMLSOCCER));
            TextReader reader = new StreamReader(@"C:\Xml.xml");
            object obj = deserializer.Deserialize(reader);
            XMLSOCCER XmlData = (XMLSOCCER)obj;
            reader.Close();
        }
Posted
Updated 5-Aug-15 0:25am
v2
Comments
Richard MacCutchan 5-Aug-15 7:42am    
It would probably work if you spellt League correctly, and consistently.

1 solution

The problem is that your class declaration does not fit the XML format...
What your code is expecting is a group of League nodes enclosed in a League node to form a kind of array:


XML
<XMLSOCCER.COM>
  <Leauge>
    <Leauge>
      ...
    </Leauge>
    <Leauge>
      ...
    </Leauge>
  </Leauge>
  ...
</XMLSOCCER.COM>

Now, you can change the structure of your XML to fit the declarations or change the declarations to fit the XML...

XML changes
XML
<XMLSOCCER.COM>
  <League>
    <League>
      <Id>1</Id>
      <Name>English Premier League</Name>
      <Country>England</Country>
      <Historical_Data>Yes</Historical_Data>
      <Fixtures>Yes</Fixtures>
      <Livescore>Yes</Livescore>
      <NumberOfMatches>5700</NumberOfMatches>
      <LatestMatch>2015-05-24T16:00:00+00:00</LatestMatch>
    </League>
    ...
  </League>
  ...
</XMLSOCCER.COM>

Code changes
C#
[Serializable()]
[XmlRoot(ElementName = "XMLSOCCER.COM", Namespace = "")]
public class XMLSOCCER
  {
  [XmlElement("Leauge")]
  public Leauge[] Leauges { get; set; }
  [XmlElement(ElementName = "AccountInformation")]
  public string AccountInformation { get; set; }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900