xml格式:
1.接受xml中的CData数据
private Hashtable GetParametersFromXML(string XMLParam,string task)
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(XMLParam);
XmlNode dataNode = xml.SelectSingleNode("ReqMsg/document");
var d = dataNode.InnerText.ToJson();
string DataNode = dataNode.InnerText;
StringBuilder sb = new StringBuilder();
sb.Append("");
sb.Append(DataNode);
XmlDocument xmls = new XmlDocument();
xmls.LoadXml(sb.ToString());
XmlNode DataNodes = xmls.SelectSingleNode(task);
Hashtable Parameters = new Hashtable();
foreach (XmlNode node in DataNodes.ChildNodes)
{
Parameters.Add(node.Name, node.InnerText);
}
return Parameters;
}
2.
/// /// 检查是否缺少参数必填参数 /// ///参数名称,多个参数用逗号隔开 ///获取到参数Hastable
public string CheckParas(string Parastr, Hashtable Paras)
{
string[] listParas = Parastr.Split(',');
string Error = "";
for (int i = 0; i < listParas.Length; i++)
{
if (listParas[i] != "")
{
if (!Paras.ContainsKey(listParas[i]))
{
Error += listParas[i] + ",";
}
}
}
if (Error != "")
{
return "缺少参数:" + Error.TrimEnd(',');
}
else
{
return "";
}
}
3./// 返回xml信息
public string GetResult(bool status, string description, string code)
{
StringBuilder sb = new StringBuilder();
sb.Append("?xml version=\"1.0\" encoding=\"UTF-8\"?");
sb.Append(""); sb.Append("");
sb.Append("APP_MES");
sb.Append("" + status.ToString() + "");
sb.Append("" + description + "");
sb.Append("");
sb.Append("");
sb.Append("[CDATA[");
sb.Append("");
sb.Append("");
return sb.ToString();
}
4.删除功能
[WebMethod]
public string DeleteTask(string xml)
{
try
{//写入数据的日志
var dirInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "WcappFiles");
if (!dirInfo.Exists)
dirInfo.Create();
var name = AppDomain.CurrentDomain.BaseDirectory + "WcappFiles/" + "DeleteTask" + new Random().Next() + ".txt";
using (var fs = new FileStream(name, FileMode.Create))
{
using (var sw = new StreamWriter(fs))
{
sw.Write(xml);
}
}
string DeleteTask = "DeleteTask";
// TODO 此处解析XML数据并处理
Hashtable paras = GetParametersFromXML(xml, DeleteTask);
//检查参数是否存在
string msg = this.CheckParas("TaskNo", paras);
int code = 00000;//00000代表数据出错
if (msg != "")
{
return GetResult(false, msg, code.ToString());
}
string TaskNo = paras["TaskNo"].ToString();
var db = new Context();
var dispatch = db.DispatchMessages.SingleOrDefault(x => x.TaskNo == TaskNo);
if (dispatch == null)
{
return GetResult(false, "任务单号不存在!", code.ToString());
}
db.DispatchMessages.Remove(dispatch);
db.SaveChanges();
return GetResult(true, "删除派工信息成功", "00001");
}
catch (Exception)
{
return GetResult(false, "删除派工信息失败", "00000");
}
}