VS技巧 根據XML自動生成型別

2021-09-06 16:20:55 字數 2676 閱讀 4966

.net 4.5對應的vs版本(不要問我哪個版本)中新增了乙個功能,嚴重實用,可以根據xml文件生成新型別。這個功能在vs的【編輯】>【選擇性貼上】選單中。怎麼玩?不急,咱們實際操作一下。

在瀏覽器位址列中輸入以上uri,然後開啟該rss源,然後檢視源。按全選選中整個xml文件。

然後回到vs專案(注意要先建乙個專案),可以新建乙個**檔案,然後把滑鼠游標定位到要插入新class的地方,然後依次執行選單【編輯】>【選擇性貼上】>【將xml貼上為類】。

然後,我們會看到神奇一幕發生。生成的**如下:

[system.xml.serialization.xmltypeattribute(anonymoustype = true

)] [system.xml.serialization.xmlrootattribute(namespace = "", isnullable = false

)]

public

partial

class

rss

set }

///[system.xml.serialization.xmlattributeattribute()]

public

decimal

version

set }

}/// [system.xml.serialization.xmltypeattribute(anonymoustype = true

)]

public

partial

class

rsschannel

set }

/// public

string

link

set }

……}

/// [system.xml.serialization.xmlelementattribute("

item")]

public

rsschannelitem item

set }

}/// [system.xml.serialization.xmltypeattribute(anonymoustype = true

)]

public

partial

class

rsschannelitem

set }

……///

public

string

pubdate

set }

/// public

rsschannelitemguid guid

set }

}/// [system.xml.serialization.xmltypeattribute(anonymoustype = true

)]

public

partial

class

rsschannelitemguid

set }

///[system.xml.serialization.xmltextattribute()]

public

string

value

set }

}

ok,**都生成了,後面大家知道怎麼做了。

static

void main(string

args)

//if (wy_rss != null

)

", wy_rss.version);

rsschannel channel =wy_rss.channel;

console.writeline(

"頻道名字:

", channel.title);

console.writeline(

"頻道描述:\n\n

", channel.description);

console.writeline(

"*****==== 資源列表 *****====");

foreach (rsschannelitem item in

channel.item)

", item.description);

console.writeline(""

, item.link);

console.writeline(

"發布日期:

", item.pubdate);

console.writeline(

"---------------------------------");}}

console.read();

}

最後,得到的結果如下圖所示。

如何,這個功能實用吧?

根據xsd生成xml文件

現在有很多的xml工具軟體都能根據xsd檔案書寫出xml文件,net 沒有實現此方法,如是我寫了幾個瀏覽 校驗 和建立xml的方法 全部 如下 using system using system.data using system.configuration using system.web usi...

根據年,自動生成日期

引數為 year,表示年份,然後自動返回當年所有的日期,格式為 date week note 20080101 星期二 null 20080102 星期三 null 20080103 星期四 null 20081228 星期日 null 20081229 星期一 null 20081230 星期二 ...

Excel 自動根據Sheet名生成目錄

查閱了相關資料現將方法分享給大家 開啟你要建立,目錄的 按alt f11在談出的對話方塊中點插入選擇模組然後把下面的 複製到模組中 sub mulu on error goto tuichu dim i as integer dim shtcount as integer dim selection...