带有嵌套标记的Xml反序列化不起作用

本文关键字:Xml 反序列化 不起作用 嵌套 | 更新日期: 2023-09-27 18:29:54

我需要将XML文件反序列化为对象。以下是XML内容:

<?xml version="1.0" encoding="utf-8" ?>
<PdfFile>
  <PageTitle DocumentName="Sequence Diagram" Version="Version 4" >Title</PageTitle>
  <LogoPath>C:'logo.png</LogoPath>
  <Modules>
    <Module Id="1" MainTitle="Module1">
      <SubModules>
        <SubModule>
          <Title>SubModule1</Title>
          <Path>SubModule1 Path</Path>
          <Description>SubModule1 Desc</Description>
        </SubModule>
        <SubModule>
          <Title>SubModule2</Title>
          <Path>SubModule2 Path</Path>
          <Description>SubModule2 Desc</Description>
        </SubModule>
      </SubModules>
    </Module>
    <Module Id="2" MainTitle="Module2">
      <SubModules>
         <SubModule>
           <Title>SubModule1</Title>
           <Path>SubModule1 Path</Path>
           <Description>SubModule1 Desc</Description>
         </SubModule>
      </SubModules>
    </Module>
  </Modules>
</PdfFile>

下面是我为上面的xml文件创建的类文件。

    using System;
    using System.Xml.Serialization;
    namespace PDFCreation.Objects
    {
        [Serializable]
        [XmlRoot("PdfFile")]
        public class PdfFile2
        {
            [XmlElement("PageTitle")]
            public PageTitle FirstPage { get; set; }
            [XmlElement("LogoPath")]
            public string LogoPath { get; set; }
            [XmlArray("Modules")]
            [XmlArrayItem("Module", typeof(Module))]
            public Module[] Modules { get; set; }
        } 
        [Serializable]
        public class Module
        {
            [XmlAttributeAttribute("Id")]
            public int Id { get; set; }
            [XmlAttributeAttribute("MainTitle")]
            public string MainTitle { get; set; }
            [XmlArray("SubModules")]
            [XmlArrayItem("SubModule", typeof(SubModule))]
            public SubModule[] Modules { get; set; }
        } 
        [Serializable]
        public class SubModule
        {
            [XmlElement("Title")]
            public string Title { get; set; }
            [XmlElement("Path")]
            public string Path { get; set; }
            [XmlElement("Description")]
            public string Description { get; set; }
        }
        [Serializable]
        public class PageTitle
        {
            [XmlText]
            public string Title { get; set; }
            [XmlAttribute]
            public string DocumentName { get; set; }
            [XmlAttribute]
            public string Version { get; set; }
        }
     }

关于反序列化,我没有得到任何错误。但是PdfFile对象内部的模块总是返回null。我试着使用从xsd.exe生成的类。但仍然发生了同样的事情。

请帮助我找到代码/xml中的问题,以及为什么它没有完全反序列化?

谢谢!!!

编辑:

我的C#代码:

  public class Program
{
    private static readonly string XmlPath = ConfigurationManager.AppSettings["XmlPath"];
    static void Main(string[] args)
    {
        try
        {
            ReadXml();
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
            Console.ReadLine();
        }
    }
    private static void ReadXml()
    {
        if (!File.Exists(XmlPath))
        {
            Console.WriteLine("Error: Xml File Not exists in the path: {0}", XmlPath);
            return;
        }
        using (var reader = new StreamReader(XmlPath))
        {
            var serializer = new XmlSerializer(typeof(PdfFile2));
            var result = (PdfFile2)serializer.Deserialize(reader);
            //other code here
        }
    }
}

带有嵌套标记的Xml反序列化不起作用

只使用您提供的代码/xml并放入丢失的结束标记,我就可以使用以下Main类对其进行反序列化:

using System.IO;
using System.Xml.Serialization;
using PDFCreation.Objects;
public class Test
{
    static void Main(string[] args)
    {
        XmlSerializer ser = new XmlSerializer(typeof(PdfFile2));
        PdfFile2 pdf = (PdfFile2)ser.Deserialize(File.OpenRead("test.xml"));
    }
}

反序列化代码是什么样子的?