public void setBussiessItems(Dictionary <string, string> keyVals) { string rootName = "param_json"; XmlDocument xmldoc = new XmlDocument(); try { string stringBussiness = this.Buy360String(this.params_360buy); string strJson = "{\"param_json\":{0}}".Replace("{0}", stringBussiness); string strXml = XML_JSON.Json2XML(strJson); xmldoc.LoadXml(strXml); XmlNode rootNode = xmldoc.SelectSingleNode(rootName); foreach (string path in keyVals.Keys) { string val = keyVals[path]; string strPath = string.Format("{0}", path); XmlNode node = rootNode.SelectSingleNode(strPath); if (node == null) { node = XmlUtilEx.CreateNode(rootNode, strPath); } node.InnerText = val; } Reset360BuyItems(xmldoc); } catch (Exception ce) { } return; }
public XmlDocument getRealXml(string strJson, out string msg) { msg = null; XmlDocument ret = new XmlDocument(); try { string retXml = XML_JSON.Json2XML(strJson); if (retXml == null) { return(null); } ret.LoadXml(retXml); XmlNode root = ret.SelectSingleNode("."); XmlNode code = root.FirstChild.SelectSingleNode("code"); if (code.InnerText != "0") { msg = code.InnerText; return(null); } XmlNode message = root.FirstChild.SelectSingleNode("message"); XmlNode result = ret.FirstChild.SelectSingleNode("result"); string res = "{\"root\":{0}}".Replace("{0}", result.InnerText.Trim()); string strResult = XML_JSON.Json2XML(res); ret.LoadXml(strResult); return(ret); } catch (Exception ce) { msg = ce.Message; return(null); } return(ret); }
public void RequestSizeAndPage(int pageSize, int page, XmlNode reqnode = null) { if (this.Module.RequestMethodUseGET) { Dictionary <string, string> obj = new Dictionary <string, string>(); obj.Add(string.Format("{0}/pageSize", this.params_360buy.First().Key), pageSize.ToString()); obj.Add(string.Format("{0}/pageIndex", this.params_360buy.First().Key), page.ToString()); this.setBussiessItems(obj); return; //this.ReqJson = string.Format("{0}&pageSize={1}&pageIndex={2}", this.ReqJson, pageSize, page); KeyValuePair <string, object> fp = this.params_360buy.First(); if (fp.Value == null) { this.params_360buy[fp.Key] = ""; } if (fp.Value.ToString().StartsWith("{") && fp.Value.ToString().EndsWith("}")) { string xmljson = "{" + string.Format("\"{0}\":{1}", fp.Key, fp.Value) + "}"; XmlDocument doc = new XmlDocument(); try { doc.LoadXml(XML_JSON.Json2XML(xmljson)); XmlNode root = doc.SelectSingleNode(fp.Key); XmlNode IndexNode = root.SelectSingleNode("pageIndex"); XmlNode SizeNode = root.SelectSingleNode("pageSize"); if (IndexNode == null) { XmlUtil.AddSubNode(root, "pageIndex", page.ToString()); } else { IndexNode.InnerText = page.ToString(); } if (SizeNode == null) { XmlUtil.AddSubNode(root, "pageIndex", pageSize.ToString()); } else { SizeNode.InnerText = pageSize.ToString(); } this.params_360buy[fp.Key] = XML_JSON.XML2Json(doc, fp.Key, true); } catch (Exception ce) { } } return; Dictionary <string, object> curr = (fp.Value as Dictionary <string, object>); if (curr.ContainsKey("pageSize")) { curr["pageSize"] = pageSize; } else { curr.Add("pageSize", pageSize); } if (curr.ContainsKey("pageIndex")) { curr["pageIndex"] = pageSize; } else { curr.Add("pageIndex", pageSize); } return; } }
public bool getDataSetXml(string json, ref XmlDocument xmldoc, ref string msg, string checkNames = "code,errcode", string errmsg = "msg") { if (xmldoc == null) { xmldoc = new XmlDocument(); xmldoc.LoadXml("<NewDataSet/>"); } if (tablist.Count == 0) { msg = "未指定指引配置!"; return(false); } string mainname = tablist[0].TableName; XmlNode rootnode = xmldoc.SelectSingleNode("NewDataSet"); if (string.IsNullOrEmpty(mainname)) { msg = "指定指引中主表名未配置!"; return(false); } try { string strXml = XML_JSON.Json2XML("{\"root\":{0}}".Replace("{0}", json)); XmlDocument tmp = new XmlDocument(); tmp.LoadXml(strXml); XmlNode tmproot = tmp.SelectSingleNode("root"); string[] errs = checkNames.Split(','); string errkey = null; for (int i = 0; i < errs.Length; i++) { if (tmproot.SelectSingleNode(errs[i]) != null) { errkey = errs[i]; break; } } if (errkey != null) { if (XmlUtil.GetSubNodeText(tmproot, errkey) != "" && XmlUtil.GetSubNodeText(tmproot, errkey) != "0")//如果错误不等于0 { msg = XmlUtil.GetSubNodeText(tmproot, string.Format("{0}", errmsg)); XmlNodeList mitemnodes = tmproot.SelectNodes(mainname); if (mitemnodes.Count == 0) { return(false); } } } XmlNamespaceManager xmlm = new XmlNamespaceManager(xmldoc.NameTable); xmlm.AddNamespace("json", "http://james.newtonking.com/projects/json");//添加命名空间 XmlNodeList itemnodes = tmproot.SelectNodes(mainname); for (int i = 0; i < itemnodes.Count; i++) { string submsg = null; if (XmlUtil.GetSubNodeText(itemnodes[0], errkey) != "" && XmlUtil.GetSubNodeText(itemnodes[i], errkey) != "0") { submsg = XmlUtil.GetSubNodeText(itemnodes[i], errmsg); msg = submsg; return(false); } //XmlNode NewRow = xmldoc.CreateElement(mainname, xmlm.LookupNamespace("json")); XmlNode NewRow = xmldoc.CreateElement(mainname); //XmlAttribute att = xmldoc.CreateAttribute("json:Array", xmlm.LookupNamespace("json")); //att.Value = "true"; //NewRow.Attributes.Append(att); rootnode.AppendChild(NewRow); foreach (XmlNode cell in itemnodes[i].ChildNodes) { if (cell.ChildNodes.Count > 1) //如果有多节点 { if (!dictabs.ContainsKey(cell.Name)) //非定义的表跳过 { continue; } AddSubTables(rootnode, cell, cell.Name, dictabs[cell.Name], itemnodes[i], mainname, xmlm);//新建表只加一层子节点,上级关键字及值传入 } else { if (cell.FirstChild is XmlElement) //如果是元素 { if (!dictabs.ContainsKey(cell.Name)) //如果未定义跳过 { continue; } AddSubTables(rootnode, cell, cell.Name, dictabs[cell.Name], itemnodes[i], mainname, xmlm);//新建表只加一层子节点及值传入 } else { cell.InnerText = cell.InnerText.Trim(); //去除空格 NewRow.AppendChild(xmldoc.ImportNode(cell, true)); //加简单节点 } } } } } catch (Exception ce) { msg = ce.Message; return(false); } return(true); }
public bool writeXmlData(XmlNode config, DataSet data, ref XmlDocument doc, ref XmlDocument xmlschema, ref string msg, string writetype = "Add") { doc = null; string strDefaultName = "DataTable1"; string strRootName = "NewDataSet"; string ret = ""; string strName = XmlUtil.GetSubNodeText(config, "module/@name"); Assembly assem = Assembly.GetExecutingAssembly(); strName = string.Format(strName, writetype); try { Type t = assem.GetType(string.Format("{0}.{1}", assem.FullName.Split(',')[0], strName)); if (t == null) { msg = "无法识别的类型"; return(false); } JdUnion_Bussiness_List_Class jdyreq = Activator.CreateInstance(t) as JdUnion_Bussiness_List_Class; ////if (jdyreq is JDYSCM_SaleOrder_Update_Class) ////{ //// msg = "销售订单外部数据不能修改,只能新增或删除!"; //// return false; ////} jdyreq.InitClass(JdUnion_GlbObject.mlist[t.Name]); jdyreq.InitRequestJson(); XmlDocument schemadoc = jdyreq.getRequestSchema(); Json2XmlClass j2x = new Json2XmlClass(schemadoc); //jdyreq.Req_PostData = getRequestJson(jdyreq,data); jdyreq.Req_PostData = j2x.getJsonString(data); if (JdUnion_GlbObject.mlist[t.Name].RequestMethodUseGET) { ret = jdyreq.GetRequest(); } else { ret = jdyreq.PostRequest(); } string strXml = XML_JSON.Json2XML("{\"root\":{0}}".Replace("{0}", ret)); XmlDocument tmp = new XmlDocument(); tmp.LoadXml(strXml); ////doc = FillXml(tmp, doc, strRootName, strDefaultName); ////xmlschema = getSchema(doc.SelectSingleNode(strRootName), strDefaultName); ////doc = ClearSubNode(doc, strRootName, strDefaultName); if (!j2x.getDataSetXml(ret, ref doc, ref msg)) { return(false); } } catch (Exception e) { msg = e.Message; return(false); } return(true); }