Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:57:23+00:00 2026-06-01T10:57:23+00:00

I need to deserialize a XML file to a object. Following is the XML

  • 0

I need to deserialize a XML file to a object. Following is the XML content:

<?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>

Following is the class file I created, for the above xml file.

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

     }

On deserializing, I’m not getting any error. But the modules inside PdfFile object always returns null. I tried to use the generated class from xsd.exe. But still the same thing is happening.

Please help me to find issue in the code/xml and why it is not deserializing fully?

Thanks!!!

Edited:

My C# code:

  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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:57:24+00:00Added an answer on June 1, 2026 at 10:57 am

    Using nothing but you supplied code/xml and putting in the missing closing tags I got it to deserialize using this 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"));
        }
    }
    

    What does your deserialization code look like?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file with the following format: <?xml version=1.0 encoding=UTF-8?> <Items> <Item
I need to deserialize a XML file to a object. The XML contents: <Players
I need to deserialize the following XML into a C# class: <NodeConfiguration> <DBChange value=2022/>
I have an Xml document: <?xml version=1.0 encoding=utf-8?> <Family xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema> <Person member=father id=0>
I have some Xml that I need to deserialize into an object. The Xml
Consider the following XML: <a> <b>2</b> <c></c> </a> I need to deserialize this xml
I am trying to deserialize a XML-file. I need to check if the XML-file
I have this Xml file, and I need to Deserialize it back to a
I have an XML document from an external source. <?xml version=1.0 encoding=utf-8?> <ns0:Info xmlns:ns0=http://www.ZomboCorp.com/>
I need to deserialize xml file to List or Array then enlarge this List

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.