在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 public static bool CreateXML() 2 { 3 XMLMapMode xmlmodel = new XMLMapMode(); 4 Dictionary<string, object> dic = new Dictionary<string, object>(); 5 xmlmodel.Code = "节点的Code属性"; 6 xmlmodel.Content = "节点的内容"; 7 xmlmodel.SQLMapFile = "文件目录"; 8 dic.Add("Description", "描述属性"); 9 dic.Add("CreateCode", "创建人"); 10 dic.Add("UpdateTime", "创建时间"); 11 xmlmodel.Parameters = dic; 12 XMLBase.UpdateXML(xmlmodel); 13 xmlModel.xpath = "/Root"; 14 bool state = CreateCDATA(xmlmodel); 15 return state; 16 } 1 public static bool CreateXmlNodeByXModel(XMLMapMode XmlModel) 2 { 3 bool isSuccess = false; 4 XmlDocument xmlDoc = new XmlDocument(); 5 try 6 { 7 xmlDoc.Load(XmlModel.SQLMapFile); //加载XML文档 8 XmlNode xmlNode = xmlDoc.SelectSingleNode(XmlModel.xpath); 9 if (xmlNode != null) 10 { 11 //存不存在此节点都创建 12 XmlElement subElement = xmlDoc.CreateElement("SQL"); 13 subElement.InnerXml = XmlModel.Content; 14 15 #region 追加属性 16 ////////追加code属性///////////////// 17 if (!string.IsNullOrEmpty(XmlModel.Code)) 18 { 19 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute("Code"); 20 xmlAttribute.Value = XmlModel.Code; 21 subElement.Attributes.Append(xmlAttribute); 22 } 23 //////////////////////// 24 25 //如果属性和值参数都不为空则在此新节点上新增其他副属性 26 if (XmlModel.Parameters.Count > 0) 27 { 28 foreach (var item in XmlModel.Parameters) 29 { 30 if (!string.IsNullOrEmpty(item.Key) && !string.IsNullOrEmpty(item.Value.ToString())) 31 { 32 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(item.Key); 33 xmlAttribute.Value = item.Value.ToString(); 34 subElement.Attributes.Append(xmlAttribute); 35 } 36 } 37 } 38 #endregion 39 40 xmlNode.AppendChild(subElement); 41 } 42 xmlDoc.Save(XmlModel.SQLMapFile); //保存到XML文档 43 isSuccess = true; 44 } 45 catch (Exception ex) 46 { 47 throw ex; //这里可以定义你自己的异常处理 48 } 49 return isSuccess; 50 } 1 private static bool CreateCDATA(XMLMapMode XmlModel) 2 { 3 bool isSuccess = false; 4 XmlDocument xmlDoc = new XmlDocument(); 5 try 6 { 7 xmlDoc.Load(XmlModel.SQLMapFile); //加载XML文档 8 XmlNode xmlNode = xmlDoc.SelectSingleNode(XmlModel.xpath); 9 if (xmlNode != null) 10 { 11 #region 创建节点 12 //遍历xpath节点下的所有子节点 13 if (!IsExistNode(XmlModel.Code, xmlNode)) 14 { 15 //存不存在此节点都创建 16 XmlElement subElement = xmlDoc.CreateElement("SQL"); 17 XmlCDataSection cdata = xmlDoc.CreateCDataSection(XmlModel.Content); 18 subElement.AppendChild(cdata); 19 //subElement.InnerXml = XmlModel.Content; 20 21 #region 追加属性 22 ////////追加code属性///////////////// 23 if (!string.IsNullOrEmpty(XmlModel.Code)) 24 { 25 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute("Code"); 26 xmlAttribute.Value = XmlModel.Code; 27 subElement.Attributes.Append(xmlAttribute); 28 } 29 //////////////////////// 30 31 //如果属性和值参数都不为空则在此新节点上新增其他副属性 32 if (XmlModel.Parameters !=null) 33 { 34 foreach (var item in XmlModel.Parameters) 35 { 36 if (!string.IsNullOrEmpty(item.Key) && !string.IsNullOrEmpty(item.Value.ToString())) 37 { 38 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(item.Key); 39 xmlAttribute.Value = item.Value.ToString(); 40 subElement.Attributes.Append(xmlAttribute); 41 } 42 } 43 } 44 #endregion 45 46 xmlNode.AppendChild(subElement); 47 48 xmlDoc.Save(XmlModel.SQLMapFile); //保存到XML文档 49 isSuccess = true; 50 } 51 else 52 { 53 isSuccess= false; 54 } 55 #endregion 56 } 57 } 58 catch (Exception ex) 59 { 60 throw ex; //这里可以定义你自己的异常处理 61 } 62 return isSuccess; 63 } 1 private static bool UpdateXmlNodeByXModel(XMLMapMode xmlModel) 2 { 3 bool isSuccess = false; 4 //bool isExistsNode = false;//标识节点是否存在 5 XmlDocument xmlDoc = new XmlDocument(); 6 try 7 { 8 xmlDoc.Load(xmlModel.SQLMapFile); //加载XML文档 9 XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlModel.xpath); 10 if (xmlNode != null) 11 { 12 XmlElement subElement = xmlDoc.CreateElement("SQL"); 13 XmlCDataSection cdata = xmlDoc.CreateCDataSection(xmlModel.Content); 14 subElement.AppendChild(cdata); 15 16 #region 追加属性 17 ////////追加code属性///////////////// 18 if (!string.IsNullOrEmpty(xmlModel.Code)) 19 { 20 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute("Code"); 21 xmlAttribute.Value = xmlModel.Code; 22 subElement.Attributes.Append(xmlAttribute); 23 } 24 //////////////////////// 25 26 //如果属性和值参数都不为空则在此新节点上新增其他副属性 27 if (xmlModel.Parameters.Count !=null) 28 { 29 foreach (var item in xmlModel.Parameters) 30 { 31 if (!string.IsNullOrEmpty(item.Key) && !string.IsNullOrEmpty(item.Value.ToString())) 32 { 33 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(item.Key); 34 xmlAttribute.Value = item.Value.ToString(); 35 subElement.Attributes.Append(xmlAttribute); 36 } 37 } 38 } 39 #endregion 40 //遍历xpath节点下的所有子节点 41 foreach (XmlNode node in xmlNode.ChildNodes) 42 { 43 string code = XMLHelper.GetNodeAttribute("Code", node); 44 if (xmlModel.Code.ToLower() == code.ToLower()) 45 { 46 //存在此节点则更新 47 xmlNode.ReplaceChild(subElement, node); 48 //isExistsNode = true; 49 break; 50 } 51 } 52 } 53 xmlDoc.Save(xmlModel.SQLMapFile); //保存到XML文档 54 isSuccess = true; 55 } 56 catch (Exception ex) 57 { 58 throw ex; //这里可以定义你自己的异常处理 59 } 60 return isSuccess; 61 } 1 public static string QueryXML(XMLMapMode xmlModel) 2 { 3 xmlModel.xpath = "/Root"; 4 string text = "当前节点无数据或不存在!"; 5 XmlDocument xmlDoc = new XmlDocument(); 6 try 7 { 8 xmlDoc.Load(xmlModel.SQLMapFile); //加载XML文档 9 XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlModel.xpath); 10 if (xmlNode != null) 11 { 12 //遍历xpath节点下的所有子节点 13 foreach (XmlNode node in xmlNode.ChildNodes) 14 { 15 string code = XMLHelper.GetNodeAttribute("Code", node); 16 if (xmlModel.Code.ToLower() == code.ToLower()) 17 { 18 text= node.InnerText; 19 } 20 } 21 } 22 } 23 catch (Exception ex) 24 { 25 text= ex.Message; //这里可以定义你自己的异常处理 26 } 27 return text; 28 } 1 private static bool DeleteXmlNodeByXModel(XMLMapMode xmlModel) 2 { 3 bool isSuccess = false; 4 //bool isExistsNode = false;//标识节点是否存在 5 XmlDocument xmlDoc = new XmlDocument(); 6 try 7 { 8 xmlDoc.Load(xmlModel.SQLMapFile); //加载XML文档 9 XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlModel.xpath); 10 if (xmlNode != null) 11 { 12 //遍历xpath节点下的所有子节点 13 foreach (XmlNode node in xmlNode.ChildNodes) 14 { 15 string code = XMLHelper.GetNodeAttribute("Code", node); 16 if (xmlModel.Code.ToLower() == code.ToLower()) 17 { 18 //存在此节点删除 19 xmlNode.RemoveChild(node); 20 //isExistsNode = true; 21 break; 22 } 23 } 24 } 25 xmlDoc.Save(xmlModel.SQLMapFile); //保存到XML文档 26 isSuccess = true; 27 } 28 catch (Exception ex) 29 { 30 throw ex; //这里可以定义你自己的异常处理 31 } 32 return isSuccess; 33 } 1 public static string GetAllFile() 2 { 3 string json = ""; 4 try 5 { 6 string path = TwiPathHelper.MapPath("文件的地址"); 7 DirectoryInfo dir = new DirectoryInfo(path); 8 FileInfo[] dirs = dir.GetFiles(); 9 if (dirs != null) 10 { 11 json = FilesToJson(dirs); 12 } 13 } 14 catch (Exception ex) 15 { 16 throw ex; 17 } 18 return json; 19 } 1 private static string XmlNodeListToJson(XmlNodeList xnl) 2 { 3 string json = "["; 4 foreach (XmlNode node in xnl) 5 { 6 string code = XMLHelper.GetNodeAttribute("Code", node); 7 string Description1 = XMLHelper.GetNodeAttribute("Description", node); 8 string Description = XMLHelper.GetNodeAttribute("Description", node); 9 string CreateTime = XMLHelper.GetNodeAttribute("CreateTime", node); 10 Description = Description + "(" + code + ")"; 11 json += "{\"Code\":@" + code + "@,\"Description1\":@" + Description1 + "@,\"Description\":@" + Description + "@,\"CreateTime\":@" + CreateTime + "@},"; 12 json=json.Replace('@', '"'); 13 } 14 json = json.TrimEnd(','); 15 json += "]"; 16 return json; 17 }
|
请发表评论