Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I rename class-names via Xml attributes?
    primarykey
    data
    text
    <p>Suppose I have an XML-serializable class called <strong>Song</strong>:</p> <pre><code>[Serializable] class Song { public string Artist; public string SongTitle; } </code></pre> <p>In order to save space (and also <em>semi-obfuscate</em> the XML file), I decide to rename the xml elements:</p> <pre><code>[XmlRoot("g")] class Song { [XmlElement("a")] public string Artist; [XmlElement("s")] public string SongTitle; } </code></pre> <p>This will produce XML output like this:</p> <pre><code>&lt;Song&gt; &lt;a&gt;Britney Spears&lt;/a&gt; &lt;s&gt;I Did It Again&lt;/s&gt; &lt;/Song&gt; </code></pre> <p>I want to rename/remap the name of the class/object as well. Say, in the above example, I wish to rename the class <strong>Song</strong> to <strong>g</strong>. So that the resultant xml should look like this:</p> <pre><code>&lt;g&gt; &lt;a&gt;Britney Spears&lt;/a&gt; &lt;s&gt;I Did It Again&lt;/s&gt; &lt;/g&gt; </code></pre> <p>Is it possible to rename class-names <strong>via xml-attributes</strong>?</p> <p>I don't wish to create/traverse the DOM manually, so I was wondering if it could be achieved via a decorator.</p> <p>Thanks in advance!</p> <p><strong>UPDATE:</strong> Oops! This time I <strong>really</strong> did it again! Forgot to mention - I'm actually serializing a list of Song objects in the XML.</p> <p>Here's the serialization code:</p> <pre><code> public static bool SaveSongs(List&lt;Song&gt; songs) { XmlSerializer serializer = new XmlSerializer(typeof(List&lt;Song&gt;)); using (TextWriter textWriter = new StreamWriter("filename")) { serializer.Serialize(textWriter, songs); } } </code></pre> <p>And here's the XML output:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ArrayOfSong&gt; &lt;Song&gt; &lt;a&gt;Britney Spears&lt;/a&gt; &lt;s&gt;Oops! I Did It Again&lt;/s&gt; &lt;/Song&gt; &lt;Song&gt; &lt;a&gt;Rihanna&lt;/a&gt; &lt;s&gt;A Girl Like Me&lt;/s&gt; &lt;/Song&gt; &lt;/ArrayOfSong&gt; </code></pre> <p>Apparently, the <strong>XmlRoot()</strong> attribute doesn't rename the object in a list context.</p> <p>Am I missing something?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload