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

  • Home
  • SEARCH
  • 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 3404042
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:20:03+00:00 2026-05-18T05:20:03+00:00

In my project, legacy code generates xml which has following structure : <Output> <Template

  • 0

In my project, legacy code generates xml which has following structure :

<Output>
    <Template recordID=12>
        <Employer type="String">
            <Value>Google</Value>
            <Value>GigaSoft inc.</Value>
        </Employer>
        <Designation  type="String">
            <Value>Google</Value>
        </Designation>
        <Duration  type="String" />
    </Template>
</Output>

I want to deserialize this xml into object which has following properties (I am using C#):

public class EmployerInfo
{
    string[] _employerName;
    string[] _designation;
    string _duration;
}

I tried to deserialize above xml using following attributes around members (NOTE : I have simplified code. I know we should not make data members public. This code is just for experimental purpose)

[XmlElement("Template")]
public class EmployerInfo
{
    [XmlElement("Employer")]
    public string[] _employerName;

    [XmlElement("Designation")]
    public string[] _designation;

    [XmlElement("Duration")]
    public string _duration;
}

To deserialize, in main class I wrote :

XmlSerializer serial = new XmlSerializer(typeof(Output));
TextReader reader = new StreamReader(@"C:\sample_xml.xml");
EmployerInfo fooBar = (EmployerInfo)serial.Deserialize(reader);
reader.Close();

After executing above code, all the members in fooBar object are set to null (default values). I think this is because xml structure does not match with class structure.

I tried to automatically generate class using xsd command but it created seperate classes for each of the data member .

I tried to give element names like XmlElement(“Employer.Value”) , XmlElement(“Template.Employer.Value”) but this also didnt work.

Can anyone please suggest some way to fit this xml into a EmployerInfo class ?

Thanks in advance

  • 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-05-18T05:20:04+00:00Added an answer on May 18, 2026 at 5:20 am

    Try:

    using System.IO;
    using System.Xml.Serialization;
    [XmlType("Template")]
    public class EmployerInfo
    {
        [XmlArray("Employer"), XmlArrayItem("Value")]
        public string[] _employerName;
    
        [XmlArray("Designation"), XmlArrayItem("Value")]
        public string[] _designation;
    
        [XmlElement("Duration")]
        public string _duration;
    }
    public class Output
    {
        public EmployerInfo Template { get; set; }
    }
    static class Program
    {
        static void Main()
        {
            XmlSerializer serial = new XmlSerializer(typeof(Output));
            using (var reader = new StringReader(@"<Output>
        <Template recordID=""12"">
            <Employer type=""String"">
                <Value>Google</Value>
                <Value>GigaSoft inc.</Value>
            </Employer>
            <Designation  type=""String"">
                <Value>Google</Value>
            </Designation>
            <Duration  type=""String"" />
        </Template>
    </Output>"))
            {
                EmployerInfo fooBar = ((Output)serial.Deserialize(reader)).Template;
            }
        }
    }
    

    Note also tat the type returned from XmlSerializer(typeof(Output))‘s deserialize method is going to be a Output record.

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

Sidebar

Related Questions

I'm working on a project that has a lot of legacy C code. We've
We are ready to start a brand new project at work, no legacy code.
I have a lot of legacy code which I currently compile using an antiquated
So, there is a legacy code that has to be imported into .NET projects.
In a legacy project that I'm on, we have several processing that are preformed
I am working on a legacy project in VC++/Win32/MFC . Recently it became a
I have a large legacy C++ project compiled under Visual Studio 2008. I know
Every project invariably needs some type of reporting functionality. From a foreach loop in
My project has both client and server components in the same solution file. I
I'm working on a legacy project, written for the most part in Delphi 5

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.