zoukankan      html  css  js  c++  java
  • 【VS技巧】根据XML自动生成类型

    .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("频道描述:
    {0}
    ", 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();
            }

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

    如何,这个功能实用吧?

  • 相关阅读:
    6个实例详解如何把if-else代码重构成高质量代码
    Fiddler抓包工具总结
    thinkphp5 图片上传七牛云
    mysql事件(定时任务)处理超时失效订单
    修改PhpStorm创建Php类文件时头部作者
    sed理论讲解、实战
    sed实战、find实战、grep实战
    通配符、正则表达式、python去重
    python3:requests模块-写了一点
    《流畅的Python》一副扑克牌中的难点
  • 原文地址:https://www.cnblogs.com/tcjiaan/p/3799975.html
Copyright © 2011-2022 走看看