XML을 역 직렬화해야합니다. chapter 요소는 여러 개의 child chapter 요소를 가질 수 있습니다. XmlSerializer를 사용하여 XML의 비 직렬화를 시도했습니다. 모든 요소는 예상대로 비 직렬화되지만 문제는 자식 챕터 배열이 비 직렬화가 아니라는 것입니다. 여기에 뭔가가 누락되어 있습니까? 도와주세요.동일한 형식의 자식 노드를 사용하는 C#에서 XML 비 직렬화하는 방법
<Survey>
<SurveyResults>
<Subject>
<Chapter>
<ChapterIterationName />
<Questions />
<Chapter>
<ChapterName>CHAPTER 1</ChapterName>
<ChapterIterationName />
<Questions>
<Question>
<Text>Question 1</Text>
</Question>
<Question>
<Text>Question 2</Text>
</Question>
</Questions>
<Chapter>
<ChapterName>CHAPTER 1.1</ChapterName>
<ChapterIterationName />
<Questions>
<Question>
<Text>Questoin 1</Text>
</Question>
<Question>
<Text>Questoin 2</Text>
</Question>
</Questions>
</Chapter>
<Chapter>
<ChapterName>CHAPTER 1.2</ChapterName>
<ChapterIterationName />
<Questions>
<Question>
<Text>Questoin 1</Text>
</Question>
<Question>
<Text>Questoin 2</Text>
</Question>
</Questions>
</Chapter>
</Chapter>
</Chapter>
</Subject>
</SurveyResults>
</Survey>
다음은 내가 시도한 코드입니다.
public class Survey
{
public SurveyResults SurveyResults { get; set; }
}
public class SurveyResults
{
public Subject Subject { get; set; }
}
public class Subject
{
public List<Chapter> Chapter { get; set; }
}
public class Chapter
{
public string ChapterName { get; set; }
public string ChapterIterationName { get; set; }
[XmlArray("Chapter")]
public List<Chapter> Chapters { get; set; }
public List<Questions> Questions { get; set; }
}
public class Questions
{
public List<Question> Question { get; set; }
}
public class Question
{
public string Text { get; set; }
}
public class Serializer
{
public T Deserialize<T>(string input) where T : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
}
}
}
Serializer ser = new Serializer();
Survey survey = ser.Deserialize<Survey>(xlString);
왜'Chapter' 대신 직접 '목록 Questions'에서'목록 Questions'을해야합니까? –