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

The Archive Base Latest Questions

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

I have the following object: public class MyClass { public int Id { get;

  • 0

I have the following object:

public class MyClass
{
    public int Id { get; set;}
    public string Name { get; set; }
}

I’m wanting to serialize this to the following xml string:

<MyClass>
    <Id>1</Id>
    <Name>My Name</Name>
</MyClass>

Unfortunately, when I use the XMLSerializer I get a string which looks like:

<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
    <Id>1</Id>
    <Name>My Name</Name>
</MyClass>

I’m not wanting MyClass to be the root element the document, rather I’m eventually wanting to add the string with other similar serialized objects which will be within a larger xml document.

i.e. Eventually I’ll have a xml string which looks like this:

<Classes>
    <MyClass>
        <Id>1</Id>
        <Name>My Name</Name>
    </MyClass>
    <MyClass>
        <Id>1</Id>
        <Name>My Name</Name>
    </MyClass>
</Classes>"

My first thought was to create a class as follows:

public class Classes
{
    public List<MyClass> MyClasses { get; set; }
}

…but that just addes an additional node called MyClasses to wrap the list of MyClass….

My gut feeling is that I’m approaching this the wrong way and that my lack of experience with creating xml files isn’t helping to point me to some part of the .NET framework or some other library that simplifies this.

  • 1 1 Answer
  • 2 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-15T02:14:44+00:00Added an answer on May 15, 2026 at 2:14 am

    You need to use the XmlWriter object – this gives you full control over how the XML will be rendered. You also need to pass a XmlSerializerNamespaces instance to the serializer to get what you need.

    The sample program below achieves exactly what you are looking for (notice how the XmlWriter is initialized with XmlWriterSettings, and how the XmlNamespaces instance is passed to the serializer):

        public class MyClass
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    

        class Program
        {
            static void Main(string[] args)
            {
                var xml = new StringBuilder();
    
                using (var writer = 
                    XmlWriter.Create(xml, 
                            new XmlWriterSettings { Indent = true, 
                              ConformanceLevel = ConformanceLevel.Auto, 
                              OmitXmlDeclaration = true }))
                {
                    MyClass myClass = new MyClass { Id = 1, Name = "My Name" };
    
                    var ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
    
                    var xs = new XmlSerializer(typeof(MyClass), "");
                    xs.Serialize(writer, myClass, ns);
                }
    
                Console.WriteLine(xml.ToString());
    
                // pause program execution to review results...
                Console.WriteLine("Press enter to exit");
                Console.ReadLine();
            }
    

    //Result:
    <MyClass>
    <Id>1</Id>
    <Name>My Name</Name>
    </MyClass>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class: public class MyClass<T> { public string TestProperty { get; set;
I have the following code: [test.h] class MyClass { public: string Name; MyClass(); void
I have the following object: public class QueueItem { public long _id { get;
I have the following two functions: public class MyClass { public void Save<TObject>(TObject object)
Given the following example class: public class MyClass { public string S { get;
I have the following code: public class MyClass { private Delegate m_action; public object[]
Assume that I have the following object public class MyClass { public ReadOnlyDictionary<T, V>
Suppose I have the following service object public class UserService { @Autowired private UserDao
I'm using ASP.NET MVC2 and I have the following object structure: public class IDealer
I have the following database table object: public class Goal { @DatabaseField(generatedId = true)

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.