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

不想你离开。 提交于 2019-12-24 19:56:27

问题


I need to deserialize bellow XML. The chapter element can have multiple child chapter element in it.I have tried to deserialize the XML using XmlSerializer. All the elements are deserializing as expected but the issue is that the child Chapter array is not deserializing, am I missing something here? Please help.

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

Here is the code I have tried.

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

Result


回答1:


[Edit]

After so many edits, I found the simple way to solve this:

  1. Copy you XML file contents.
  2. From Visual Studio Menus select Edit -> Paste Special -> Paste XML as Classes.
  3. Use the deserialize code.

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


来源:https://stackoverflow.com/questions/47765992/how-to-deserialize-xml-in-c-sharp-having-child-nodes-of-same-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!