3

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);
    }

}
8
  • The XmlSerializer is looking for (for example) <ArrayOfOdgovor><Odgovor /><Odgovor /></ArrayOfOdgovor>. It will deserialize an <ArrayOfFoo /> into a Foo[] array or a List<Foo>. But I don't know of a way to get it to do that in the absence of ArrayOfFoo elements in the source. Commented Mar 26, 2012 at 23:00
  • I edit a XML document, but the program still does not work. Commented Mar 27, 2012 at 17:53
  • Hi Zoran, I suspect you need to decorate the Odgovor.odgovor property with the [XmlText] attribute. Commented Mar 27, 2012 at 18:02
  • I add [XmlElement("Odgovor")], but the program still does not work :( Commented Mar 27, 2012 at 20:23
  • when you decorate the odgovor property with [XmlElement("Odgovor")] you're telling the serializer that the Odgovor element contains an Odgovor element of type string, that is, something like this: <Odgovori><Odgovor tacan = "true"><Odgovor>Madrid</Odgovor></Odgovor></Odgovori>. Use [XmlText] to indicate that the odgovor property corresponds to the Text content of the <Odgovor> element; that will match your sample XML data. Commented Mar 27, 2012 at 20:27

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have edit XML file & C# code, I also add <Odgovori>, but program still not working, can you help me?
0

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

Comments

0

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()
    };

1 Comment

Thank you for help, but I must use XML Deserialization, can you help me with that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.