using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections;
///
/// DrugQuery 的摘要描述
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
// [System.Web.Script.Services.ScriptService]
public class DrugQuery : System.Web.Services.WebService {
DrugDataSystemRef.wsDrugDataSystem systemWS = new DrugDataSystemRef.wsDrugDataSystem();
public DrugQuery()
{
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
/**
* 取得 症狀名稱查詢資料
**/
[WebMethod]
public String GetDrugDataListByIndication(String indication)
{
// 讀取從 它方 webservice
indication = systemWS.GetDrugDataListByIndication(indication.Replace(" ",""));
// ============================================
// load and handle xml format
// ============================================
XmlDocument doc = new XmlDocument();
doc.LoadXml(indication);
XmlDocument afterParseDoc = stripDocumentNamespace(doc);
// 取xml內code與msg
XmlNode code = afterParseDoc.SelectSingleNode("/string/error/code");
XmlNode msg = afterParseDoc.SelectSingleNode("/string/error/msg");
// 取row 的node list
XmlNodeList rowList = afterParseDoc.SelectNodes("/string/data/row");
// 使用Dictionary記錄code與name值
Dictionary drugDataDic = new Dictionary();
foreach (XmlNode xn in rowList)
{
if( xn["code"].InnerText != null || xn["code"].InnerText != "" )
{
drugDataDic.Add(xn["code"].InnerText.Trim(), xn["name"].InnerText.Trim());
}
}
// ===========================================
// handle xml to json format
// ===========================================
// 建立物件
JObject jObj = new JObject();
// 建立屬性 值
jObj.Add(new JProperty("result", code.InnerText.Trim()));
jObj.Add(new JProperty("message", msg.InnerText.Trim()));
JArray jAry = new JArray();
// 建立resultData Array
jObj.Add(new JProperty("resultData", jAry));
foreach( KeyValuePair kvp in drugDataDic )
{
// 建立resultData內部物件A
JObject resObj = new JObject();
// 新增屬性值
resObj.Add("code", kvp.Key.Trim());
resObj.Add("name", kvp.Value.Trim());
// 將物件A加入Array
jAry.Add(resObj);
}
// 輸出json格式
String output = JsonConvert.SerializeObject(jObj);
return output;
}
/**
* 取得 症狀藥品名稱查詢資料
**/
[WebMethod]
public string GetDrugDataListByName(String name)
{
// 取得查詢結果
name = systemWS.GetDrugDataListByName(name.Replace(" ", ""));
// ============================================
// load and handle xml format
// ============================================
// parse xml查詢結果
XmlDocument doc = new XmlDocument();
doc.LoadXml(name);
XmlDocument afterParseDoc = stripDocumentNamespace(doc);
XmlNode code = afterParseDoc.SelectSingleNode("/string/error/code");
XmlNode msg = afterParseDoc.SelectSingleNode("/string/error/msg");
XmlNodeList rowList = afterParseDoc.SelectNodes("/string/data/row");
// 使用Dictionary記錄code與name值
Dictionary drugDataDic = new Dictionary();
foreach (XmlNode xn in rowList)
{
if (xn["code"].InnerText.Trim() != null || xn["code"].InnerText.Trim() != "")
{
drugDataDic.Add(xn["code"].InnerText.Trim(), xn["name"].InnerText.Trim());
}
}
// ===========================================
// handle xml to json format
// ===========================================
// 建立物件
JObject jObj = new JObject();
// 建立屬性 值
jObj.Add(new JProperty("result", code.InnerText.Trim()));
jObj.Add(new JProperty("message", msg.InnerText.Trim()));
JArray jAry = new JArray();
// 建立resultData Array
jObj.Add(new JProperty("resultData", jAry));
foreach (KeyValuePair kvp in drugDataDic)
{
// 建立resultData內部物件A
JObject resObj = new JObject();
// 新增屬性值
resObj.Add("code", kvp.Key.Trim());
resObj.Add("name", kvp.Value.Trim());
// 將物件A加入Array
jAry.Add(resObj);
}
// 輸出json格式
String output = JsonConvert.SerializeObject(jObj);
return output;
}
/**
* 取得 uk查詢資料
**/
[WebMethod]
public string GetDrugData(String uk)
{
uk = systemWS.GetDrugData(uk.Trim());
// ============================================
// load and handle xml format
// ============================================
XmlDocument doc = new XmlDocument();
doc.LoadXml(uk);
XmlDocument afterParseDoc = stripDocumentNamespace(doc);
XmlNode errCode = afterParseDoc.SelectSingleNode("/string/error/code");
XmlNode errMsg = afterParseDoc.SelectSingleNode("/string/error/msg");
XmlNodeList rowList = afterParseDoc.SelectNodes("/string/data/row");
List ukDataList = new List();
for( int i = 0 ; i < rowList.Count ; i++ )
{
if (rowList[i].SelectSingleNode("code").InnerText.Trim() != null || rowList[i].SelectSingleNode("code").InnerText.Trim() != "")
{
SetUkData setUkData = new SetUkData();
setUkData.CodeValHandler = rowList[i].SelectSingleNode("code").InnerText.Trim();
setUkData.NameValHandler = rowList[i].SelectSingleNode("name").InnerText.Trim();
setUkData.ViewValHandler = rowList[i].SelectSingleNode("view").InnerText.Trim();
setUkData.PictureValHandler = rowList[i].SelectSingleNode("picture1").InnerText.Trim();
setUkData.FactorValHandler = rowList[i].SelectSingleNode("factor").InnerText.Trim();
setUkData.AdaptValHandler = rowList[i].SelectSingleNode("adapt").InnerText.Trim();
setUkData.AtendValHandler = rowList[i].SelectSingleNode("attend").InnerText.Trim();
setUkData.SideEffectValHandler = rowList[i].SelectSingleNode("side_effect").InnerText.Trim();
ukDataList.Add(setUkData);
}
}
// ===========================================
// handle xml to json format
// ===========================================
// 建立物件
JObject jObj = new JObject();
// 建立屬性 值
jObj.Add(new JProperty("result", errCode.InnerText.Trim()));
jObj.Add(new JProperty("message", errMsg.InnerText.Trim()));
JArray jAry = new JArray();
// 建立resultData Array
jObj.Add(new JProperty("resultData", jAry));
for( int i = 0 ; i < ukDataList.Count ; i++ )
{
// 建立resultData內部物件A
JObject resObj = new JObject();
// 新增屬性值
resObj.Add("code", ukDataList[i].CodeValHandler.Trim());
resObj.Add("name", ukDataList[i].NameValHandler.Trim());
resObj.Add("view", ukDataList[i].ViewValHandler.Trim());
resObj.Add("picture1", ukDataList[i].PictureValHandler.Trim());
resObj.Add("factor", ukDataList[i].FactorValHandler.Trim());
resObj.Add("adapt", ukDataList[i].AdaptValHandler.Trim());
resObj.Add("attend", ukDataList[i].AtendValHandler.Trim());
resObj.Add("side_effect", ukDataList[i].SideEffectValHandler.Trim());
// 將物件A加入Array
jAry.Add(resObj);
}
String output = JsonConvert.SerializeObject(jObj);
return output;
}
// remove namespace string
XmlDocument stripDocumentNamespace(XmlDocument oldDom)
{
// Remove all xmlns:* instances from the passed XmlDocument
// to simplify our xpath expressions.
XmlDocument newDom = new XmlDocument();
newDom.LoadXml(System.Text.RegularExpressions.Regex.Replace(
oldDom.OuterXml, @"(xmlns:?[^=]*=[""][^""]*[""])", "",
System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline)
);
return newDom;
}
// 讀寫 UK的內容值
class SetUkData
{
private String codeVal;
private String nameVal;
private String viewVal;
private String pictureVal;
private String factorVal;
private String adaptVal;
private String atendVal;
private String sideEffectVal;
public String CodeValHandler
{
get
{
return codeVal;
}
set
{
this.codeVal = value;
}
}
public String NameValHandler
{
get
{
return nameVal;
}
set
{
this.nameVal = value;
}
}
public String ViewValHandler
{
get
{
return viewVal;
}
set
{
this.viewVal = value;
}
}
public String PictureValHandler{
get{
return pictureVal;
}
set{
this.pictureVal = value;
}
}
public String FactorValHandler
{
get
{
return factorVal;
}
set
{
this.factorVal = value;
}
}
public String AdaptValHandler
{
get
{
return adaptVal;
}
set
{
this.adaptVal = value;
}
}
public String AtendValHandler
{
get
{
return atendVal;
}
set
{
this.atendVal = value;
}
}
public String SideEffectValHandler
{
get
{
return sideEffectVal;
}
set
{
this.sideEffectVal = value;
}
}
}
}
2013年5月27日 星期一
webservice - xml to json(C#)
此文章您無權限
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言