Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

c# - How can I rename class-names via Xml attributes?

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.

Thanks in advance!

UPDATE: Oops! This time I really did it again! Forgot to mention - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution: Use [XmlType(TypeName="g")]

XmlRoot only works with XML root nodes as per the documentation (and what you would expect, given its name includes root)!

I was unable to get any of the other answers to work so kept digging...

Instead I found that the XmlTypeAttribute (i.e. [XmlType]) and its TypeName property do a similar job for non-root classes/objects.

e.g.

[XmlType(TypeName="g")]
class Song
{
    public string Artist;
    public string SongTitle;
}

Assuming you apply it to the other classes e.g.:

[XmlType(TypeName="a")]
class Artist
{
    .....
}

[XmlType(TypeName="s")]
class SongTitle
{
    .....
}

This will output the following exactly as required in the question:

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

I have used this in several production projects and found no problems with it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...