嵌套标记无效的Xml反序列化

时间:2012-04-03 10:54:39

标签: c# xml xml-deserialization

我需要将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生成的类。但仍然发生同样的事情。

请帮我在code / 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
        }
    }
}

1 个答案:

答案 0 :(得分:3)

除了你提供的代码/ 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"));
    }
}

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