using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace PTMedicalInsurance
{
class AnalysisXML
{
///
/// XML转换JSON对象
///
///
///
///
public JObject ConvertXMLToObject(string sXML)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(sXML);
string jsonStr = JsonConvert.SerializeXmlNode(xmlDoc);
jsonStr = jsonStr.Replace("@", "").ToString();
//MessageBox.Show(jsonStr);
return JObject.Parse(jsonStr);
}
///
/// 判断XML是否存在元素
///
///
///
///
/// 0字符串 1金额
///
public string FormatXMLElement(XmlDocument xmldoc, XmlNode xmlnode, string element, int Type)
{
if ((Type != 0) && (Type != 1))
{
Type = 0;
}
string key = "";
key = "//" + element;
if (xmldoc.SelectSingleNode(key) != null)
{
return xmlnode.SelectNodes(element)[0].InnerText;
}
else
{
if (Type == 0)
return "";
else
return "0.00";
}
}
public XmlDocument GetXmlDoc(string sXML)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(sXML);
return xmlDoc;
}
public XmlNode GetNodeFromPath(XmlNode oParentNode, string sPath)
{
XmlNode tmpNode = oParentNode.SelectNodes(sPath)[0];
return tmpNode;
}
public bool CheckState(XmlDocument xmlDoc,out string OutMsg)
{
OutMsg = "";
string sState = GetNodeFromPath(xmlDoc.DocumentElement, "state").Attributes["success"].InnerText;
bool bRet = false;
if (sState.Equals("true"))
{
bRet = true;
//AddLog("调用返回状态:成功");
}
else
{
bRet = false;
//AddLog("调用返回状态:失败");
//读取错误信息
XmlNodeList errNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("error");
string sErrNO = "", sErrMsg = "";
for (int i = 0; i < errNodes.Count; i++)
{
if (errNodes[i].Attributes.Count > 0)
{
sErrNO = errNodes[i].Attributes["no"].InnerText;
sErrMsg = errNodes[i].Attributes["info"].InnerText;
//AddLog("调用返回错误:编号 [" + sErrNO + "] -- 描述 [" + sErrMsg + "]");
}
}
//读取警告信息
string sWarNO = "", sWarMsg = "";
XmlNodeList warNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("warning");
for (int i = 0; i < warNodes.Count; i++)
{
if (warNodes[i].Attributes.Count > 0)
{
sWarNO = warNodes[i].Attributes["no"].InnerText;
sWarMsg = warNodes[i].Attributes["info"].InnerText;
//AddLog("调用返回警告:编号 [" + sWarNO + "] -- 描述 [" + sWarMsg + "]");
}
}
if ((sWarNO != "") || (sWarMsg != "")|| (sErrNO != "") || (sErrMsg != ""))
{
OutMsg = "错误编号:" + sErrNO + "\n" +
"错误信息:" + sErrMsg + "\n" +
"警告编号:" + sWarNO + "\n" +
"警告信息:" + sWarMsg + "\n";
}
}
//读取信息
//XmlNodeList infNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("information");
//for (int i = 0; i < infNodes.Count; i++)
//{
// if (infNodes[i].Attributes.Count > 0)
// {
// string sInfNO = infNodes[i].Attributes["no"].InnerText;
// string sInfMsg = infNodes[i].Attributes["info"].InnerText;
// //AddLog("调用返回信息:编号 [" + sInfNO + "] -- 描述 [" + sInfMsg + "]");
// }
//}
return bRet;
}
//public string bSucess(XmlDocument xmlDoc, string ItemName)
//{
// if (Node(ItemName) <> null)
//}
//public string GetXMLValue(string aXmlText, string FieldName)
//{
//}
public bool CheckOutputState(XmlDocument xmlDoc)
{
string sState = GetNodeFromPath(xmlDoc.DocumentElement, "state").Attributes["success"].InnerText;
bool bRet = false;
if (sState.Equals("true"))
{
bRet = true;
//AddLog("调用返回状态:成功");
}
else
{
bRet = false;
//AddLog("调用返回状态:失败");
}
//读取错误信息
XmlNodeList errNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("error");
for (int i = 0; i < errNodes.Count; i++)
{
if (errNodes[i].Attributes.Count > 0)
{
string sErrNO = errNodes[i].Attributes["no"].InnerText;
string sErrMsg = errNodes[i].Attributes["info"].InnerText;
//AddLog("调用返回错误:编号 [" + sErrNO + "] -- 描述 [" + sErrMsg + "]");
}
}
//读取警告信息
XmlNodeList warNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("warning");
for (int i = 0; i < warNodes.Count; i++)
{
if (warNodes[i].Attributes.Count > 0)
{
string sWarNO = warNodes[i].Attributes["no"].InnerText;
string sWarMsg = warNodes[i].Attributes["info"].InnerText;
//AddLog("调用返回警告:编号 [" + sWarNO + "] -- 描述 [" + sWarMsg + "]");
}
}
//读取信息
XmlNodeList infNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("information");
for (int i = 0; i < infNodes.Count; i++)
{
if (infNodes[i].Attributes.Count > 0)
{
string sInfNO = infNodes[i].Attributes["no"].InnerText;
string sInfMsg = infNodes[i].Attributes["info"].InnerText;
//AddLog("调用返回信息:编号 [" + sInfNO + "] -- 描述 [" + sInfMsg + "]");
}
}
return bRet;
}
}
}