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 8863115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:53:45+00:00 2026-06-14T15:53:45+00:00

Below is the my XML file. <Employee> <FirstName>#{FirstName}#</FirstName> <LastName>#{LastName}#</LastName> <DOB>#{DOB}#</DOB> <Address>#{Address}#</Address> <Transcation> <Month>#{Month}#</Month> <Amount>#{Amount}#</Amount>

  • 0

Below is the my XML file.

<Employee>
    <FirstName>#{FirstName}#</FirstName>
    <LastName>#{LastName}#</LastName>
    <DOB>#{DOB}#</DOB>
    <Address>#{Address}#</Address>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>

and my serializable class is

[XmlRoot("Employee"), Serializable]    
 public class Employee    
 {    
     [XmlAnyElement]
      public List<XmlElement> EmployeeDetails    { get; set; }
 }

But i want to get something like this

In my EmployeeDetails i should serialize only FirstName, LastName, DOB, Address,…. and I should get List of Transcation in a separate class which contains Month and Amount as serialiazable element.

something like this

[XmlRoot("Employee"), Serializable]    
public class Employee    
{    
 [XmlAnyElement]
 public List<XmlElement> EmployeeDetails    { get; set; }

 [XmlElement("Transcation")]
 public List<Transcation> Transcations { get; set; }

}

public class Transcation
{
  [XmlElement("Month")]
    public string Month{ get; set; }
  [XmlElement("Amount")]
    public string Amount{ get; set; }

}

How can i do this ?

  • 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-14T15:53:47+00:00Added an answer on June 14, 2026 at 3:53 pm

    Assuming you can’t modify the XML your class should look like the following. XmlAnyElement can be used however you will probably need to set it as an object array. So your code should look like this:

    [XmlRoot("Employee"), Serializable]
    public class Employee
    {
        [XmlAnyElement]
        public XmlElement [] EmployeeDetails { get; set; }
    
        [XmlElement("Transcation")]
        public List<Transaction> Transcations { get; set; }
    }
    

    To Deserialize you do the following:

        private void DeserializeObject(string filename)
        {
            XmlAnyElementAttribute myAnyElement = new XmlAnyElementAttribute();
            XmlAttributeOverrides xOverride = new XmlAttributeOverrides();
            XmlAttributes xAtts = new XmlAttributes();
            xAtts.XmlAnyElements.Add(myAnyElement);
            xOverride.Add(typeof(Employee), "EmployeeDetails", xAtts);
    
            XmlSerializer ser = new XmlSerializer(typeof(Employee), xOverride);
    
            FileStream fs = new FileStream(filename, FileMode.Open);
            var g = (Employee)ser.Deserialize(fs);
            fs.Close();
    
            Console.WriteLine(g.EmployeeDetails.Length);
            foreach (XmlElement xelement in g.EmployeeDetails)
            {
                Console.WriteLine(xelement.Name + ": " + xelement.InnerXml);
            }
        }
    

    I recommend instead to specify the properties, it will make it easier to look for specific values. But this depends on what you know about the xml schema before hand.

    [XmlRoot(ElementName="Employee")]    
    public class Employee    
    {    
      [XmlElement(ElementName="FirstName")]    
      public string FirstName {get;set;}
    
      [XmlElement(ElementName="LastName")]
      public string LastName {get;set;}
    
      [XmlElement(ElementName="DOB")]
      public DateTime DOB {get;set;}
    
      [XmlElement(ElementName="Address")]
      public string Address {get;set;}
    
      [XmlElement(ElementName="Transaction")]
      public List<Transaction> Transaction {get;set;}
    }
    
    
    [XmlRoot(ElementName="Transaction")]
    public class Transaction
    {
    
      [XmlElement(ElementName="Month")]
      public int Month {get;set;}
    
      [XmlElement(ElementName="Amount")]
      public int Amount {get;set;}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to read the below xml file and write its content into a plain
How do I change my XML file below to have CAS return to the
I already implemented to create the XML file below with XmlTextWriter when application initialization.
I have an xml file likes below. <?xml version=1.0 encoding=utf-8 ?> <Book> <Title>Title</Title> <Content>Content</Content>
I'm traversing an xml file as below: <?xml version=1.0 encoding=ISO-8859-1?> <wrapper> <site base=http://www.example1.co.uk/ name=example
I have a xml file as below: <?xml version=1.0 encoding=UTF-8 standalone=no?> <RULES> <RULE DESCRIPTION=
I setup a logging.xml file as shown below. This file includes output to the
I have a flat structured XML file as below: <rs> <r id=r1 lev=0/> <r
I have a sample xml file which looks like the one below: <Root> <SubOne>
I am having a xml file similar to below. <Parameters> <Parameter IsEncrypted=False> <ParameterName>pRdGpInstallOptions</ParameterName> <ParameterValue>Custom</ParameterValue>

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.