微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 嵌套标签无效的Xml反序列化

我需要将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
        }
    }
}

解决方法:

只使用你提供的代码/ xml并输入缺少的结束标记,我使用这个Main-class反序列化:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。