2015年4月24日 星期五

[C#] 含藥化粧品詳細處方成分資料集 -以成份做分類

將以下網址
http://data.fda.gov.tw/frontsite/data/DataAction.do?method=doDetail&infoId=43





















XML下載下來





































接下來用C#程式處理資料


 using System;  
 using System.Collections.Generic;  
 using System.ComponentModel;  
 using System.Data;  
 using System.Drawing;  
 using System.IO;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 using System.Windows.Forms;  
 using System.Xml;  
 using System.Collections;  
 namespace Data  
 {  
   public partial class Form1 : Form  
   {  
     public Form1()  
     {  
       InitializeComponent();  
     }  
     List<dataXML> list43 = new List<dataXML>();  
     Hashtable element = new Hashtable();  
     FileStream fs = new FileStream("element_class.txt", FileMode.Create);  
     StreamWriter sw;  
     private void button1_Click(object sender, EventArgs e)  
     {  
       //讀取  
       read43();  
       //寫檔  
       sw = new StreamWriter(fs);  
       writeFile();  
       sw.Flush();  
       //關閉流  
       sw.Close();  
       fs.Close();  
     }  
     private void writeFile()  
     {  
       dataXML data;  
       ArrayList akeys = new ArrayList(element.Keys); //別忘了導入System.Collections  
       akeys.Sort(); //按字母順序進行排序  
       foreach (string skey in akeys)  
       {  
         if (skey.Trim() == String.Empty)  
         {  
           continue;  
         }  
         ArrayList array= (ArrayList)element[skey];  
         sw.Write(skey+"\n\n");  
         for (int i=0;i<array.Count;i++)  
         {  
           data=(dataXML)array[i];  
           sw.WriteLine(data.許可證字號);  
         }  
         sw.WriteLine("\n");  
       }  
     }  
     private void read43()  
     {  
       Hashtable hs =new Hashtable();  
       dataXML d43;  
       XmlDocument xd = new XmlDocument();  
       //Load裡放xml的路徑就可去讀取  
       xd.Load("43_1.xml");  
       //可將xml裡的<rows>元素都存到nodelist裡  
       XmlNodeList nodelist = xd.SelectNodes("//rows");  
       //再用迴圈去跑出來  
       foreach (XmlNode item_File in nodelist)  
       {  
         XmlNodeList childnodelist = item_File.ChildNodes;  
         d43 = new dataXML();  
         foreach (XmlNode childXmlNode in childnodelist)  
         {  
           switch (childXmlNode.Name)  
           {  
             case "許可證字號":  
               d43.許可證字號 = childXmlNode.InnerText.Replace("'", "''");  
               break;  
             case "處方標示":  
               d43.處方標示 = childXmlNode.InnerText.Replace("'", "''");  
               break;  
             case "成分名稱":  
               d43.成分名稱 = childXmlNode.InnerText.Replace("'", "''");  
               break;  
             case "含量描述":  
               d43.含量描述 = childXmlNode.InnerText.Replace("'", "''");  
               break;  
             case "含量":  
               d43.含量 = childXmlNode.InnerText.Replace("'", "''");  
               break;  
             case "含量單位":  
               d43.含量單位 = childXmlNode.InnerText.Replace("'", "''");  
               break;  
             default:  
               break;  
           }  
         }  
         if (!element.ContainsKey(d43.成分名稱))  
         {  
           ArrayList array = new ArrayList();  
           array.Add(d43);  
           element.Add(d43.成分名稱, array);  
         }  
         else  
         {  
           ArrayList oldArray =(ArrayList) element[d43.成分名稱];  
           element.Remove(d43.成分名稱);  
           oldArray.Add(d43);  
           element.Add(d43.成分名稱, oldArray);  
         }  
         list43.Add(d43);  
       }  
     }  
     struct dataXML  
     {  
       public string 許可證字號;  
       public string 處方標示;  
       public string 成分名稱;  
       public string 含量描述;  
       public string 含量;  
       public string 含量單位;  
     }  
   }  
 }  


執行結果

沒有留言:

張貼留言