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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:49:53+00:00 2026-05-16T15:49:53+00:00

Suppose I have an XML-serializable class called Song : [Serializable] class Song { public

  • 0

Suppose I have an XML-serializable class called Song:

[Serializable]
class Song
{
    public string Artist;
    public string SongTitle;
}

In order to save space (and also semi-obfuscate the XML file), I decide to rename the xml elements:

[XmlRoot("g")]
class Song
{
    [XmlElement("a")]
    public string Artist;
    [XmlElement("s")]
    public string SongTitle;
}

This will produce XML output like this:

<Song>
  <a>Britney Spears</a>
  <s>I Did It Again</s>
</Song>

I want to rename/remap the name of the class/object as well. Say, in the above example, I wish to rename the class Song to g. So that the resultant xml should look like this:

<g>
  <a>Britney Spears</a>
  <s>I Did It Again</s>
</g>

Is it possible to rename class-names via xml-attributes?

I don’t wish to create/traverse the DOM manually, so I was wondering if it could be achieved via a decorator.

I’m actually serializing a list of Song objects in the XML.

Here’s the serialization code:

    public static bool SaveSongs(List<Song> songs)
    {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Song>));
            using (TextWriter textWriter = new StreamWriter("filename"))
            {
                serializer.Serialize(textWriter, songs);
            }
    }

And here’s the XML output:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSong>
<Song>
  <a>Britney Spears</a>
  <s>Oops! I Did It Again</s>
</Song>
<Song>
  <a>Rihanna</a>
  <s>A Girl Like Me</s>
</Song>
</ArrayOfSong>

Apparently, the XmlRoot() attribute doesn’t rename the object in a list context.

Am I missing something?

  • 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-16T15:49:53+00:00Added an answer on May 16, 2026 at 3:49 pm

    Checkout the XmlRoot attribute.

    Documentation can be found here:
    http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlrootattribute(v=VS.90).aspx

    [XmlRoot(Namespace = "www.contoso.com", 
         ElementName = "MyGroupName", 
         DataType = "string", 
         IsNullable=true)]
    public class Group
    

    UPDATE:
    Just tried and it works perfectly on VS 2008.
    This code:

    [XmlRoot(ElementName = "sgr")]
    public class SongGroup
    {
        public SongGroup()
        {
           this.Songs = new List<Song>();
        }
    
    
    
    [XmlElement(ElementName = "sgs")]
        public List<Song> Songs { get; set; }
    }
    
    [XmlRoot(ElementName = "g")]
    public class Song
    {
        [XmlElement("a")]
        public string Artist { get; set; }
    
        [XmlElement("s")]
        public string SongTitle { get; set; }
    } 
    

    Outputs:

    <?xml version="1.0" encoding="utf-8"?>
    <sgr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www
    .w3.org/2001/XMLSchema">
      <sgs>
        <a>A1</a>
        <s>S1</s>
      </sgs>
      <sgs>
        <a>A2</a>
        <s>S2</s>
      </sgs>
    </sgr>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I have this object: [Serializable] public class MyClass { public int Age
Suppose I have a simple Java class like this: public class User { String
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
Suppose I have this XML as a string: <calles> <calle> <nombre>CALLAO AV.</nombre> <altura>1500</altura> <longitud>-58.3918617027</longitud>
Possible Duplicate: How to Deserialize XML document Suppose that I have a class that
suppose i have xml data stored in string variable. so when i am trying
Suppose i have one customer class and i will serialize the class to xml
Suppose I have this XML document... <root> <str name=My node's attribute>My string's value</str> </root>
Suppose I have a stateless bean: @Stateless(name = fooBean) @LocalBean public class Foo {
Suppose I have an XML document stored as an Anti-XML Elem : val root

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.