【VS技巧】根据XML自动生成类型

发布时间:2014-06-21作者: 邯郸翱翔

.NET 4.5对应的VS版本(不要问我哪个版本)中新增了一个功能,严重实用,可以根据XML文档生成新类型。这个功能在VS的【编辑】>【选择性粘贴】菜单中。怎么用?不急,咱们实际操作一下。以网易

.NET 4.5对应的VS版本(不要问我哪个版本)中新增了一个功能,严重实用,可以根据XML文档生成新类型。这个功能在VS的【编辑】>【选择性粘贴】菜单中。怎么用?不急,咱们实际操作一下。

以网易新闻中心的RSS源为例,URI必须指向XML文档,我选用了“文化资讯”频道的内容来测试,URI如下:

http://book.163.com/special/0092451H/rss_whzx.xml

 

在浏览器地址栏中输入以上URI,然后打开该RSS源,然后查看源。按全选选中整个XML文档。

然后回到VS项目(注意要先建一个项目),可以新建一个代码文件,然后把鼠标光标定位到要插入新class的地方,然后依次执行菜单【编辑】>【选择性粘贴】>【将XML粘贴为类】。

然后,我们会看到神奇一幕发生。生成的代码如下:

复制代码
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class rss
    {

        ……/// <remarks/>
        public rssChannel channel
        {
            get
            {
                return this.channelField;
            }
            set
            {
                this.channelField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal version
        {
            get
            {
                return this.versionField;
            }
            set
            {
                this.versionField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class rssChannel
    {

        ……/// <remarks/>
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }

        /// <remarks/>
        public string link
        {
            get
            {
                return this.linkField;
            }
            set
            {
                this.linkField = value;
            }
        }

        ……
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("item")]
        public rssChannelItem[] item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class rssChannelItem
    {

        ……/// <remarks/>
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }

……/// <remarks/>
        public string pubDate
        {
            get
            {
                return this.pubDateField;
            }
            set
            {
                this.pubDateField = value;
            }
        }

        /// <remarks/>
        public rssChannelItemGuid guid
        {
            get
            {
                return this.guidField;
            }
            set
            {
                this.guidField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class rssChannelItemGuid
    {

        private bool isPermaLinkField;

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public bool isPermaLink
        {
            get
            {
                return this.isPermaLinkField;
            }
            set
            {
                this.isPermaLinkField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }
复制代码

 

OK,代码都生成了,后面大家知道怎么做了。

这里我举个例子,通过代码在线获得RSS源的XML文档,然后通过XML反序列化来得到一个刚才生成的rss类的实例,然后就像访问其他普通类型一样使用。

复制代码
        static void Main(string[] args)
        {
            // 设置控制台窗口的缓冲区大小
            Console.SetBufferSize(Console.LargestWindowWidth, 1000);
            // 获取XML的URI
            string uri = "http://book.163.com/special/0092451H/rss_whzx.xml";
            WebClient wc = new WebClient();
            // 获取RSS内容
            byte[] xmlData = wc.DownloadData(uri);
            rss wy_rss = null;
            using (MemoryStream ms=new MemoryStream(xmlData))
            {
                // 反序列化
                XmlSerializer xs = new XmlSerializer(typeof(rss));
                wy_rss = (rss)xs.Deserialize(ms);
            }
            // 如果反序列化成功,则输出相关内容
            if (wy_rss != null)
            {
                Console.WriteLine("版本:{0}", wy_rss.version);
                rssChannel channel = wy_rss.channel;
                Console.WriteLine("频道名字:{0}", channel.title);
                Console.WriteLine("频道描述:\n{0}\n", channel.description);
                Console.WriteLine("========= 资源列表 =========");
                foreach (rssChannelItem item in channel.item)
                {
                    Console.WriteLine("标题:{0}", item.title);
                    Console.WriteLine("描述:{0}", item.description);
                    Console.WriteLine("链接:{0}", item.link);
                    Console.WriteLine("发布日期:{0}", item.pubDate);
                    Console.WriteLine("---------------------------------");
                }
            }
            Console.Read();
        }
复制代码

最后,得到的结果如下图所示。


关于我们
翱翔简介
青鸟简介
诚聘精英
在线咨询
热门课程
BCSP软件开发专业
云计算专业
大数据专业
Web前端专业
java开发专业
翱翔就业
就业案例
翱翔荣誉
微信 公众号 在线咨询 免费课程