2012-04-03 5 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で問題を見つけるのを手伝ってください、なぜ完全にデシリアライズしていないのですか?

ありがとうございました!何を使用していない

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

の終了タグが欠けているようです。デシリアライゼーションコードを表示できますか? – paul

+0

申し訳ありませんXMLを正しくフォーマットし、デシリアライゼーションコードを追加しました。それを指してくれてありがとう。 –

答えて

3

ていますが、コード/ XMLと私はそれがこのメイン・クラスを使用してデシリアライズするようになった行方不明閉じタグに入れての供給:

編集:

私のC#コード

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

デシリアライズコードはどのように見えますか?

関連する問題