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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:14:15+00:00 2026-05-26T03:14:15+00:00

I want to create custom xml serialization by implementing IXmlSerializable. I’ve got this test

  • 0

I want to create custom xml serialization by implementing IXmlSerializable.
I’ve got this test class that implements IXmlSerializable interface:

[Serializable]
public class Employee : IXmlSerializable
{
    public Employee()
    {
        Name = "Vyacheslav";

        Age = 23;                 
    }

    public string Name{get; set;}

    public int Age { get; set; }



    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        this.Name = reader["Name"].ToString();
        this.Age = Int32.Parse(reader["Age"].ToString());
    }

    public void WriteXml(XmlWriter writer)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.ConformanceLevel = ConformanceLevel.Fragment;

        XmlWriter newWriter = XmlWriter.Create(writer, settings);
        newWriter.WriteAttributeString("Name", this.Name);
        newWriter.WriteAttributeString("Age", this.Age.ToString());            
    }
}

What I want to do is to omit xml declaration. For that I create proper instance of XmlWriterSettings and pass it as second parameter to create new XmlWriter.
But when I debug this piece of code, I see that newWriter.Settings.OmitXmlDeclaration is set to false and serialized data contains tag. What am I doing wrong?

The actual serialization looks like this:

        var me = new Employee();

        XmlSerializer serializer = new XmlSerializer(typeof(Employee));
        TextWriter writer = new StreamWriter(@"D:\file.txt");
        serializer.Serialize(writer, me);
        writer.Close();

And the second question is – if I want to serialize type Employee that has cutom type ContactInfo at field to be serialized, do I need to implement IXmlSerializable on ContactInfo too?

  • 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-26T03:14:15+00:00Added an answer on May 26, 2026 at 3:14 am

    The writer-settings is a function of the outermost writer; you should be applying that to the code that creates the file, i.e.

    using(var file = File.Create("file.txt"))
    using(var writer = XmlWriter.Create(file, settings))
    {
        serializer.Serialize(writer, me);
    }
    

    additionally, then, you don’t need to implement IXmlSerializable. You cannot do this at the inner level – it is too late.

    For example:

    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;
    public class Employee 
    {
        [XmlAttribute] public string Name { get; set; }
        [XmlAttribute] public int Age { get; set; }
    }
    class Program
    {
        static void Main()
        {
            var settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            var me = new Employee {
                Name = "Vyacheslav", Age = 23
            };
            var serializer = new XmlSerializer(typeof (Employee));
            using (var file = File.Create("file.txt"))
            using (var writer = XmlWriter.Create(file, settings))
            {
                serializer.Serialize(writer, me);
            }
        }
    }
    

    and if you don’t want the extra namespaces, then:

            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            serializer.Serialize(writer, me, ns);
    

    which generates the file:

    <Employee Name="Vyacheslav" Age="23" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a custom MSBuild task that changes my .cs files before
I want to create a custom set that will automatically convert objects into a
I want to create a custom list in Flex for an interface prototype. The
If you want to create a custom attribute for MS test (say [Repeat(3)] how
I want to create a custom XML structure as follows: <Hotels> <Hotel /> </Hotels>
I want to create custom tag, but i get XML parsing error on JSPVersion
I want to create a custom View which contains two buttons without using xml.
i want to create a custom view class. But I get an error by
I want to create a web method that accepts a List of custom objects
I want to create a custom control in C#. But every time I have

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.