【问题标题】:How to Deserialize XML in c# having child nodes of same type如何在 c# 中反序列化具有相同类型子节点的 XML
【发布时间】:2017-12-12 05:40:10
【问题描述】:

我需要反序列化下面的 XML。章节元素中可以有多个子章节元素。我尝试使用 XmlSerializer 反序列化 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 中有List&lt;Questions&gt; Questions 而不是直接List&lt;Question&gt; Questions

标签: c# xml xml-serialization xml-deserialization


【解决方案1】:

[编辑]

经过这么多修改,我找到了解决这个问题的简单方法:

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

【讨论】:

  • 不工作,子章节不存在且问题计数 = 0。已使用输出更新问题。
  • @KousiK 我编辑了我的答案,现在可以了。看看吧!
  • 没有名为 的元素。 元素嵌套到另一个 。我不是在创建 XML,而是从第三方获取 xml。
  • 哦。我以为你创造了它。我又错了:)
  • @Bassed Akl,我只需要将 xml 文件解析为 C# 对象。
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
  • 2022-06-27
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
相关资源
最近更新 更多