.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(); }
最后,得到的結果如下圖所示。
如何,這個功能實用吧?