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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:34:38+00:00 2026-05-13T14:34:38+00:00

I am trying to change the root name when doing XML serialization with C#.

  • 0

I am trying to change the root name when doing XML serialization with C#.

It always takes the class name and not the name I am trying to set it with.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            MyTest test = new MyTest();
            test.Test = "gog";

            List<MyTest> testList = new List<MyTest>() 
            {    
                test 
            }; 

            SerializeToXML(testList);
        }

        static public void SerializeToXML(List<MyTest> list)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<MyTest>));
            TextWriter textWriter = new StreamWriter(@"C:\New folder\test.xml");
            serializer.Serialize(textWriter, list);
            textWriter.Close();
        }
    }


}





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ConsoleApplication1
{

    [XmlRootAttribute(ElementName = "WildAnimal", IsNullable = false)]
    public class MyTest
    {
        [XmlElement("Test")]
        public string Test { get; set; }


    }
}

Result

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMyTest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyTest>
    <Test>gog</Test>
  </MyTest>
</ArrayOfMyTest>

It does not change it to WildAnimal. I am not sure why. I got this from a tutorial.

Edit @ Marc

Thanks. I now see what your doing just seems so odd that you have to make a wrapper around it. I have one more question what happens if I wanted to make this format

<root>
   <element>
        <name></name>
   </element>
   <anotherElement>
       <product></product> 
   </anotherElement>
</root>

so like a nested elements. Would I have to make a new class for the second part and stick that in the wrapper class 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-13T14:34:38+00:00Added an answer on May 13, 2026 at 2:34 pm

    In your example, MyTest is not the root; are you trying to rename the array? I would write a wrapper:

    [XmlRoot("NameOfRootElement")]
    public class MyWrapper {
        private List<MyTest> items = new List<MyTest>();
        [XmlElement("NameOfChildElement")]
        public List<MyTest> Items { get { return items; } }
    }
    
    static void Main() {
        MyTest test = new MyTest();
        test.Test = "gog";
    
        MyWrapper wrapper = new MyWrapper {
            Items = {  test }
        };
        SerializeToXML(wrapper);
    }
    
    static public void SerializeToXML(MyWrapper list) {
        XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
        using (TextWriter textWriter = new StreamWriter(@"test.xml")) {
            serializer.Serialize(textWriter, list);
            textWriter.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to change the color of a DataGridTextColumn. Here's what I'm doing: <DataGridTextColumn
I am trying to change the connectionString in my web.config using MSBUILD on a
Disclaimer: the following is a sin against XML. That's why I'm trying to change
I have an XML structure like the following <root> <person> <name>James</name> <description xsi:type=me:age>12</description> <description
Just trying to use Java to rename the root of a folder tree. Using
I am trying to change my web-app's JDBC code to JPA using Hibernate as
I'm trying change an input mask for textbox when the the check box has
Am trying to change the button frame according to the orientation change but button
I´m trying to change a spring jsp example to use freemarker. I changed all
im trying to change font globally for whole application, the problem is, i am

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.