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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:37:26+00:00 2026-05-24T20:37:26+00:00

I try to serialize a class, defined as following: [DataContract] public class Human {

  • 0

I try to serialize a class, defined as following:

        [DataContract]
    public class Human
    {

        public Human() { }


        public Human(int id, string Name, bool isEducated)
        {
            this.id = id;
            this.Name = Name;
            this.isEducated = isEducated;
        }

        [DataMember]
        public int id { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public bool isEducated { get; set; }
    }

Then it’s being serialized:

            Human h = new Human(id, Name, isEducated);
        XmlRootAttribute root = new XmlRootAttribute();
        root.ElementName = "Repository"; 
        XmlSerializer xs = new XmlSerializer(typeof(Human), root);

        FileStream fs = new FileStream(fname, FileMode.Open);
        xs.Serialize(fs, h);

And this is the outcome:

 <Repository xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <id>1</id>
  <Name>Vill</Name>
  <isEducated>false</isEducated>
</Repository>

Not something I’d expect. Class name is just omited. What is the problem 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-05-24T20:37:27+00:00Added an answer on May 24, 2026 at 8:37 pm

    You are explicitly naming the root element “Repository”, which is why it is showing up that way. Try omitting that statement 🙂

    I think what you want is to have Human after this Repository element. To do that, you will need to make another root element below the Repository one and then serialize your object into that. You should note that the XmlSerializer is typically used to write objects to xml files, not really create entire xml files. I have an example of something that might work for you below:

    System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(FilePath, null);
    writer.WriteStartDocument();
    writer.Formatting = Formatting.Indented;
    writer.WriteStartElement("Repository");
    
    Human h = new Human(id, Name, isEducated);
    XmlRootAttribute root = new XmlRootAttribute();
    root.ElementName = "Human"; 
    XmlSerializer xs = new XmlSerializer(typeof(Human), root);
    xs.Serialize(writer, h);
    
    writer.WriteEndElement();
    writer.Close();
    

    Something like that…I’m really sleepy :/

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

Sidebar

Related Questions

Just imagine you have the following class [DataContract] public class NamedList { [DataMember] public
When I try to serialize class with protected members, I get the following errors:
When I try to serialize a populated instance of type List<C>() where: public class
How to I serialize an object like this with protobuf-net: public class MyObject{ public
I'm getting the following error when I try to Serialize an HttpWebRequest Type 'System.Net.KnownHttpVerb'
Try the following code public enum Color { Blue=1, Red=2, Green=3 } public List<Color>
Here's my code: [Serializable()] public class Project { private List<string> _Kinds = new List<string>();
I have defined: [RdfSerializable] public class SomeItem { // Unique identificator of the resource
I want to serialize an object. I've got this basic class structure: class Controller
please help. I have this code, it's my class to serialize\deserialize application settings. [XmlRoot(EvaStartupData)]

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.