如何重命名类名通过XML属性? [英] How can I rename class-names via Xml attributes?

查看:225
本文介绍了如何重命名类名通过XML属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为XML序列化类的歌曲

Suppose I have an XML-serializable class called Song:

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

为了节省空间(也的半模糊处理的XML文件),我决定重命名XML元素:

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;
}

这样这将产生XML输出​​:

This will produce XML output like this:

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

我要重命名/重新映射类的名称/对象为好。再说了,在上面的例子中,我想这个类的歌曲重命名为。这样生成的XML应该是这样的:

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>

是否有可能重新命名类名的通过XML的属性

我不希望创建/手动遍历DOM的,所以我在想,如果它可以通过装饰来实现。

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

在此先感谢!

更新:哎呀!这一次,我的真的再次做到了!
忘了说 - 我居然在XML序列化宋对象的列表

UPDATE: Oops! This time I really did it again! Forgot to mention - I'm actually serializing a list of Song objects in the XML.

下面是序列化code:

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);
            }
    }

和这里的XML输出​​:

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>

显然,在 XmlRoot()属性并不在列表上下文重命名的对象。

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

我缺少的东西吗?

推荐答案

结帐的XmlRoot属性。

Checkout the XmlRoot attribute.

文档可以在这里找到:
<一href=\"http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlrootattribute(v=VS.90).aspx\">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

更新:
只是尝试,它完美的作品在2008年VS。
这code:

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; }
} 

输出:

<?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>

这篇关于如何重命名类名通过XML属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆