2012-04-03 1 views
4

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에서 생성 된 클래스를 사용하려고했습니다. 그러나 여전히 똑같은 일이 일어나고 있습니다.

코드/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 
     } 
    } 
} 
+0

전체 XML입니까? 닫기 태그 (위에 지적한) 이외의 '' – Tung

+0

의 닫기 태그가 누락 된 것 같습니다. 모두 괜찮아 보입니다. deserialization 코드를 보여줄 수 있습니까? – paul

+0

죄송합니다! 형식화 된 XML을 올바르게 추가하고 역 직렬화 코드를 추가했습니다. 그것을 가리켜 주셔서 감사합니다. –

답변

3

만을 사용하지만, 공급 코드/XML이 메인 클래스 사용하여 직렬화하는 겠어요 누락 닫는 태그에 넣어 :

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

비 직렬화 코드는 어떻게 생겼습니까?