只有累積,沒有奇蹟

2019年2月18日 星期一

[VisualStudio] Paste Special - 將 Json 或 XML 文字轉換為 C# 類別物件

問題 
這陣子開發工作上常遇到要與第三方做串接的服務,第三方服務提供的技術文件中格式不外乎以 Json 或是 XML 兩種為主,往往都要針對這些屬性自己建立新的類別 Model 的動作,如果 XML 或 JSON 屬性欄位多的話要花不少時間在處理建立對應類別上,但不用擔心,號稱地表上最強的開發 IDE Visual Studio 從 2012 開始提供 Json 與 XML 轉 Class 類別的功能,操作與使用上相當簡單,這篇就來介紹此功能的使用方式。 

使用方式 
打開 Visual Studio 點選左上角 Edit > Paste Special,就可以看到可以將剪貼簿內容的文字轉換為 Json 或是 XML 的選項,以下就針對兩個選項做簡單的介紹與 Sample

Json 轉 Class
Json 檔案範例是從 .NET Core 專案的 appsetting.json 抓下來,如下所示
  1. {
  2. "ConnectionStrings": {
  3. "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-ASPCoreLab;Trusted_Connection=True;MultipleActiveResultSets=true"
  4. },
  5. "Logging": {
  6. "LogLevel": {
  7. "Default": "Warning"
  8. }
  9. },
  10. "AllowedHosts": "*"
  11. }
Step 1 : 打開 Visual Studio 2017,建立一個新的 Class 類別,接著複製要轉換的 Json 檔案
Step 2 : Edit > Paste Special > Paste Json to Class
  1. public class Rootobject
  2. {
  3. public Connectionstrings ConnectionStrings { get; set; }
  4. public Logging Logging { get; set; }
  5. public string AllowedHosts { get; set; }
  6. }
  7.  
  8. public class Connectionstrings
  9. {
  10. public string DefaultConnection { get; set; }
  11. }
  12.  
  13. public class Logging
  14. {
  15. public Loglevel LogLevel { get; set; }
  16. }
  17.  
  18. public class Loglevel
  19. {
  20. public string Default { get; set; }
  21. }
會產生 RootObject 類別物件,其中包含要轉換 Json 類別的屬性 connectionString、logging、Loglevel 其相對應的 Class 類別

XML 轉 Class
自己定義一個簡單的 XML 檔案,如下所示
  1. <card>
  2. <name>Marcus Tung</name>
  3. <title>打雜工程師</title>
  4. <email>iamnewuser@gmail.com</email>
  5. <phone>886 82 5252</phone>
  6. <logo url="widget.gif"/>
  7. </card>
Step 1 : 打開 Visual Studio 2017,建立一個新的 Class 類別,接著複製要轉換的 XML 檔案
Step 2 : Edit > Paste Special > Paste XML to Class
  1. // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
  2. /// <remarks/>
  3. [System.SerializableAttribute()]
  4. [System.ComponentModel.DesignerCategoryAttribute("code")]
  5. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  6. [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
  7. public partial class card
  8. {
  9.  
  10. private string nameField;
  11.  
  12. private string titleField;
  13.  
  14. private string emailField;
  15.  
  16. private string phoneField;
  17.  
  18. private cardLogo logoField;
  19.  
  20. /// <remarks/>
  21. public string name
  22. {
  23. get
  24. {
  25. return this.nameField;
  26. }
  27. set
  28. {
  29. this.nameField = value;
  30. }
  31. }
  32.  
  33. /// <remarks/>
  34. public string title
  35. {
  36. get
  37. {
  38. return this.titleField;
  39. }
  40. set
  41. {
  42. this.titleField = value;
  43. }
  44. }
  45.  
  46. /// <remarks/>
  47. public string email
  48. {
  49. get
  50. {
  51. return this.emailField;
  52. }
  53. set
  54. {
  55. this.emailField = value;
  56. }
  57. }
  58.  
  59. /// <remarks/>
  60. public string phone
  61. {
  62. get
  63. {
  64. return this.phoneField;
  65. }
  66. set
  67. {
  68. this.phoneField = value;
  69. }
  70. }
  71.  
  72. /// <remarks/>
  73. public cardLogo logo
  74. {
  75. get
  76. {
  77. return this.logoField;
  78. }
  79. set
  80. {
  81. this.logoField = value;
  82. }
  83. }
  84. }
  85.  
  86. /// <remarks/>
  87. [System.SerializableAttribute()]
  88. [System.ComponentModel.DesignerCategoryAttribute("code")]
  89. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  90. public partial class cardLogo
  91. {
  92.  
  93. private string urlField;
  94.  
  95. /// <remarks/>
  96. [System.Xml.Serialization.XmlAttributeAttribute()]
  97. public string url
  98. {
  99. get
  100. {
  101. return this.urlField;
  102. }
  103. set
  104. {
  105. this.urlField = value;
  106. }
  107. }
  108. }
會產生 Rool XML Card 類別物件,並且會自動幫你預設定義好 attribute,像是 Serializable、XML Type 等相關定義,Card 底下的五個屬性有分別定義相關的 get / set,算是非常實用 !

心得
使用地表上最強 IDE 開發十分方便,連這小細節在早期就開始提供,但知道此功能的人似乎不多有點可惜就是,日後如果有遇到一些不錯的功能在陸續介紹在 Blog 上給需要的朋友,Happy Coding !!

Related Posts:

  • [VS2017] 提升 Resharper 在 Visual Studio IDE 執行速度前言  Visual Studio 是地表上最強的開發工具,最近發現在安裝完 Resharper 後開啟專案時都會遇到 載入專案的速度變慢的問題,Visual Studio 會在上方提示 JetBrains ReSharper Ultimate’ likely caused 8 seconds of unresponsiveness. Disabling it may improve your experi… Read More
  • [UnitTest] Reflect.cs not found in NUnit問題 最近在新的專案寫 UnitTest 過程中,執行完後 Visual Studio 卻跳出 Source Not Fount : Reflect.cs not found 錯誤,但單元測試還是可以成功,如下圖 且透過 debug UnitTest 時發現測試的值也沒問題,經搜尋後發現是 Visual Studio 2017 IDE 設定問題,將,這篇文章簡單紀錄處理問題過程 處理方式 釐清問題 按下… Read More
  • [UnitTest] Visual Studio 2017 新增單元測試時沒有 NUnit 選項 ?前言 新筆電總是讓人充滿驚喜,在使用上除了要重新安裝常用的開發工具外,像是 Visual Studio & SQL Server..等必須工具,還有些常用的套件與小工具也要一併安裝,NUnit 是蠻常用的 Test Framwroek 之一,在寫測試時在要測試的 method 下按下右鍵 create Unit Tests,選擇要使用測試的 Test Framework 即可,在測試專案中預設只有 MSTest 選項,如果需要 NUnit… Read More
  • [VS2017] 如何修改 Visual Studio IDE 顯示語系 前言 目前筆電使用的 Visual Studio 是繁體中文版,在使用上用起來沒英文版那麼順,很多字詞在翻譯上總是讓人覺得驚喜,忍無可忍無須再忍決定改為英文版,過去 VS 可以透過下載的方式安裝語言包,但這招在 Visual Studio 2017 似乎無效(?,這邊簡單紀錄 Visual Studio 如何安裝不同語系的過程 設定語系 IDE 語系設定  設定方式 : 工具 > 選項 > 環境… Read More
  • [UnitTest] Visual Studio 2017 按右鍵無法建立單元測試 ? 問題 最近心血來潮使用家中舊電腦小白寫 Code,在練習測試中發現竟然有點怪異,在要測試的 method 按下右鍵沒有 建立單元測試 Create Unit Test 選項,但相同練習專案拿到公司筆電就是正常的,經比對後發現舊筆電 Visual Studio 版本少安裝測試功能,以下簡單紀錄解決問題的過程 解決方案 在 Visual Studio 2017 早期版本這是已知問題,有開發者在 vs community 回報給開… Read More

0 意見:

張貼留言

Copyright © 2025 m@rcus 學習筆記 | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com