繁体   English   中英

如何在具有相同类型的子节点的C#中反序列化XML

[英]How to Deserialize XML in c# having child nodes of same type

我需要反序列化以下XML。 章元素可以包含多个子章元素。我尝试使用XmlSerializer反序列化XML。 所有元素都按预期方式反序列化,但是问题是子Chapter数组未反序列化,我在这里缺少什么吗? 请帮忙。

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

结果 产量

[编辑]

经过如此多的编辑后,我找到了解决此问题的简单方法:

  1. 复制您的XML文件内容。
  2. 从Visual Studio菜单中选择“ 编辑”->“选择性粘贴”->“将XML粘贴为类”
  3. 使用反序列化代码。

      string path = @"G:\\Projects\\StackOverFlow\\WpfApp1\\Survey.xml"; FileStream reader = File.OpenRead(path); XmlSerializer ser = new XmlSerializer(typeof(Survey)); Survey survey = (Survey)ser.Deserialize(reader); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM