Reputation: 485
I have searched forever tryng to figure out what I am doing wrong in trying to deseralize some xml to a list. I am able to get the deserailzation of eveything but the answers, so the deserailization is working but it appears I am missing something on the class decorations. The answers show in the "testObj", but are null.
Any help is appreciated.
Sample Xml
<?xml version="1.0" encoding="utf-8" ?>
<TestObj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<question>
<stem>this is the question stem</stem>
<answers>
<answer>answer 1</answer>
<answer>answer 2</answer>
<answer>answer 3</answer>
<answer>answer 4</answer>
</question>
<question .... </question>
<question .... </question>
<question .... </question>
</TestObj>
[Serializable]
public class TestObj
{
[XmlElement(ElementName = "question")]
[XmlElement(typeof(QuestionObj))]
public List<QuestionObj> Questions { get; set; }
public int Id { get; set; }
public string Name;
}
[Serializable]
public class QuestionObj
{
[XmlElement(ElementName = "stem")]
public string Stem { get; set; }
[XmlArray("answers")]
[XmlArrayItem(ElementName = "answer")]
[XmlArrayItem(typeof(AnswerObj))]
public List<AnswerObj> Answers { get; set; }
public int TestId { get; set; }
public int Id { get; set; }
}
[Serializable]
public class AnswerObj
{
[XmlElement(ElementName = "answer")]
public string Answer { get; set; }
public int Id { get; set; }
public int StemId { get; set; }
}
Object returned:
Question:this is the question stem
answers
answer:null
answer null;
etc
Upvotes: 0
Views: 895
Reputation: 141
Sorry, but I need to ask. Are you initializing the AnswerObj properly, and set the answer in it? In a XmlArrayItem you will get another object in it not just a string answer.
Upvotes: 0
Reputation: 271
To get xml file you expecting for, try this one:
[Serializable]
public class TestObj
{
[XmlElement(ElementName = "question")]
[XmlElement(typeof(QuestionObj))]
public List<QuestionObj> Questions { get; set; }
public int Id { get; set; }
public string Name;
}
[Serializable]
public class QuestionObj
{
[XmlElement(ElementName = "stem")]
public string Stem { get; set; }
[XmlElement("answers")]
public List<AnswerObj> Answers { get; set; }
public int TestId { get; set; }
public int Id { get; set; }
}
[Serializable]
public class AnswerObj
{
[XmlElement(ElementName = "answer")]
public string Answer { get; set; }
public int Id { get; set; }
public int StemId { get; set; }
}
Upvotes: 0
Reputation: 46077
Here's a good example from Marc Gravell here:
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot("user_list")]
public class UserList
{
public UserList() {Items = new List<User>();}
[XmlArray("Items")]
[XmlArrayItem("User", typeof(User))]
public List<User> Items {get;set;}
}
public class User
{
[XmlElement("id")]
public Int32 Id { get; set; }
[XmlElement("name")]
public String Name { get; set; }
}
static class Program
{
static void Main()
{
XmlSerializer ser= new XmlSerializer(typeof(UserList));
UserList list = new UserList();
list.Items.Add(new User { Id = 1, Name = "abc"});
list.Items.Add(new User { Id = 2, Name = "def"});
list.Items.Add(new User { Id = 3, Name = "ghi"});
ser.Serialize(Console.Out, list);
}
}
Upvotes: 1
Reputation: 906
Your serialization attributes are making the serializer look for an xml structure like this:
<question>
<answers>
<answer>
<answer>answer text</answer>
<Id>1234</Id>
<StemId>1234</StemId>
</answer>
</answers>
</question>
You can either change the serializer to use a List<string>
for the answers or construct an AnswerObj in xml for each answer (as per above).
Upvotes: 2