返回異常和成功的兩種不同,XML返回直接拿alibaba_aliqin_fc_sms_num_send_response判斷節點是否有這個名字
官方API地址:
https://api.alidayu.com/doc2/apiDetail?spm=a3142.7629140.1999205496.19.fs37Yu&apiId=25450

<?xml version="1.0" encoding="utf-8" ?> <alibaba_aliqin_fc_sms_num_send_response> <result> <err_code>0</err_code> <model>102330458199^1102997158000</model> <success>true</success> </result> <request_id>z28wpa7p4jz3</request_id> </alibaba_aliqin_fc_sms_num_send_response> <!--top010178001118.n.et2-->

<?xml version="1.0" encoding="utf-8" ?> <error_response> <code>50</code> <msg>Remote service error</msg> <sub_code>isv.invalid-parameter</sub_code> <sub_msg>非法參數</sub_msg> </error_response>

public static SortedDictionary<string, object> AlidayuFromXml(string xml) { SortedDictionary<string, object> obj = new SortedDictionary<string, object>(); if (!string.IsNullOrEmpty(xml)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); //如果傳入的節點個數要比index大才行,要不然就表示節點不存在 if (xmlDoc.ChildNodes.Count > 1) { XmlNode xmlNode = xmlDoc.ChildNodes[1];//獲取到根節點<xml> XmlElement xeinit = (XmlElement)xmlNode; if (xmlNode.ChildNodes.Count == 2 || xeinit.Name == "alibaba_aliqin_fc_sms_num_send_response") { XmlNode xmlNodeNext = xmlNode.ChildNodes[0]; XmlNodeList nodes = xmlNodeNext.ChildNodes; foreach (XmlNode xn in nodes) { XmlElement xe = (XmlElement)xn; obj[xe.Name] = xe.InnerText;//獲取xml的鍵值對到WxPayData內部的數據中 } } else { XmlNodeList nodes = xmlNode.ChildNodes; foreach (XmlNode xn in nodes) { XmlElement xe = (XmlElement)xn; obj[xe.Name] = xe.InnerText;//獲取xml的鍵值對到WxPayData內部的數據中 } } } } return obj; }
返回對象后
var model = AlidayuFromXml(rsp.Body.ToString()); if (model.ContainsKey("success")) { //編寫成功之后要進行的事務 }else{ }