Zoran Đukić
Zoran Đukić

Reputation: 777

XML deserialization C# array

I want to import questions for quiz which are in XML file. There are list of questions in XML, and every questions have a list of answers. Can someone help me and tell me where I´m wrong?

XML file "pitanja.xml":

<?xml version="1.0" encoding="utf-8"?>

<Pitanja>

  <Pitanje>

    <TekstPitanja>
      U kojoj državi se nalazi Ajfelova kula?
    </TekstPitanja>

    <Odgovori>
      <Odgovor tacan = "true" > Francuska </Odgovor>
      <Odgovor> Nemačka </Odgovor>
      <Odgovor> Španija </Odgovor>
      <Odgovor> Italija </Odgovor>
    </Odgovori>

  </Pitanje>

  <Pitanje>

    <TekstPitanja>
      U kom gradu se nalazi Big Ben?
    </TekstPitanja>

    <Odgovori>
      <Odgovor tacan = "true" > London </Odgovor>
      <Odgovor> Pariz </Odgovor>
      <Odgovor> Lisabon </Odgovor>
      <Odgovor> Madrid </Odgovor>
    </Odgovori>

  </Pitanje>

  <Pitanje>

    <TekstPitanja>
      Glavni grad Španije je?
    </TekstPitanja>

    <Odgovori>
      <Odgovor tacan = "true" > Madrid </Odgovor>
      <Odgovor> Barselona </Odgovor>
      <Odgovor> Lisabon </Odgovor>
      <Odgovor> Rim </Odgovor>
    </Odgovori>

  </Pitanje>

</Pitanja>

C# code:

[XmlRoot("Pitanja")]
public class Pitanja
{
    [XmlArray("Pitanja")]
    [XmlArrayItem("Pitanje")]
    public List<Pitanje> SvaPitanja { get; set; }

}

public class Pitanje
{
    [XmlElement("TekstPitanja")]
    public string TekstPitanja { get; set; } // Tekst pitanja

    [XmlArray("Odgovori")]
    [XmlArrayItem("Odgovor")]
    public List<Odgovor> Odgovori { get; set; }    // Niz odgovora na pitanje

}

public class Odgovor
{
    [XmlText]
    public string odgovor { get; set; }

    [XmlAttribute]
    public Boolean tacan { get; set; }

}

public void ucitajpitanja()
{
    XmlSerializer dsr = new XmlSerializer(typeof(Pitanja));
    using (System.IO.StreamReader str = new System.IO.StreamReader(@"C:\pitanja.xml"))
    {
        pitanja = (Pitanja)dsr.Deserialize(str);
    }

}

Upvotes: 3

Views: 3150

Answers (3)

000
000

Reputation: 806

If you are not restricted to XML Serialization and your objective is simply to read the XML file into your object model, then as an alternative I suggest considering using Linq to XML.

As a sample, your XMLfile could be read into your classes using the following code:

    var result = new Pitanja
    {
        SvaPitanja = (from pitanje in System.Xml.Linq.XDocument.Load(@"C:\pitanja.xml").Root.Elements()
                        select new Pitanje
                        {
                            TekstPitanja = pitanje.Element("TekstPitanja").Value.Trim(),
                            Odgovori = (from odgovor in pitanje.Elements("Odgovor")
                                        let tacanAttribute = odgovor.Attribute("tacan")
                                        select new Odgovor
                                        {
                                            odgovor = odgovor.Value.Trim(),
                                            tacan = tacanAttribute != null && tacanAttribute.Value == "true"
                                        }).ToList()
                        }).ToList()
    };

Upvotes: 0

Hupperware
Hupperware

Reputation: 979

Both the Array and the object must have deserializers.

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

    [XmlAttribute]
    public Boolean tacan { get; set; }
 }

[Serializable]
[XmlRoot("Pitanja", Namespace = "", IsNullable = false)]
public class PitanjaModelList {
   [XmlElementAttribute("Pitanje", Form = XmlSchemaForm.Unqualified)]
   public List<Pitanje> PitanjaList { get; set; }
}

The accepted answer here: Convert XML String to Object will give you how to generate a perfect deserializer for your XML

Upvotes: 0

Lubomir Velkov
Lubomir Velkov

Reputation: 56

I think you need to wrap the <Odgovor> elements in an additional element - e.g. <Odgovori>

Upvotes: 1

Related Questions