我一直在试图找出我在尝试将一些xml解封到列表时出错了。我能够得到eveything的deserailisation但答案,所以deserailization正在工作,但似乎我在课堂装饰上遗漏了一些东西。答案显示在“testObj”中,但是为空。
感谢任何帮助。
示例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
答案 0 :(得分:2)
您的序列化属性使序列化程序查找xml结构,如下所示:
<question>
<answers>
<answer>
<answer>answer text</answer>
<Id>1234</Id>
<StemId>1234</StemId>
</answer>
</answers>
</question>
您可以更改序列化程序以使用List<string>
作为答案,或者为每个答案构建一个xml的AnswerObj(如上所述)。
答案 1 :(得分:1)
以下是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);
}
}
答案 2 :(得分:0)
要获取您期望的xml文件,请尝试以下方法:
[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; }
}
答案 3 :(得分:0)
抱歉,我需要问一下。您是否正确初始化AnswerObj并在其中设置答案? 在XmlArrayItem中,您将获得其中的另一个对象,而不仅仅是字符串答案。