using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Web.Model.DBModel;
namespace Web.Helper
{
public class XmlHelper
{
public static readonly string XmlPath = System.Web.HttpContext.Current.Server.MapPath("/Config/CustomGroupListJXML.xml");
/// <summary>
/// 查询所有的Text
/// </summary>
/// <returns></returns>
public List<string> SelectAllText()
{
List<string> list = new List<string>();
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(XmlPath);
XmlNode xn = xmlDocument.SelectSingleNode("CustomGroupList");
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xn1 in xnl)
{
XmlElement xe = (XmlElement)xn1;
if (xe.GetAttribute("Text")!="系统默认")
{
list.Add(xe.GetAttribute("Text"));
}
}
return list;
}
/// <summary>
/// 根据节点的text来读取所有子节点
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public Dictionary<string,string> ReadXmlByText(string text=null)
{
try
{
Dictionary<string, string> returnDic = new Dictionary<string, string>();
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(XmlPath);
XmlNode xn = xmlDocument.SelectSingleNode("CustomGroupList");
XmlNodeList xnl = xn.ChildNodes;
List<string> list = new List<string>();
foreach (XmlNode xn1 in xnl)
{
XmlElement xe = (XmlElement)xn1;
if (!string.IsNullOrEmpty(text))
{
if (xe.GetAttribute("Text").ToString().IndexOf(text) > -1)
{
XmlNodeList xnl0 = xe.ChildNodes;
for (int i = 0; i < xnl0.Count; i++)
{
returnDic.Add(xnl0.Item(i).Name, xnl0.Item(i).InnerText);
}
}
}
}
return returnDic;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 增加节点
/// </summary>
/// <param name="nodeName"></param>
/// <param name="childNode"></param>
/// <returns></returns>
public string AddXmlNode(string nodeName, Dictionary<string, string> childNode)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(XmlPath);
XmlNode root = xmlDocument.SelectSingleNode("CustomGroupList");
XmlElement xelKey = xmlDocument.CreateElement("AllFields");
XmlAttribute xelType = xmlDocument.CreateAttribute("Text");
xelType.InnerText = nodeName;
xelKey.SetAttributeNode(xelType);
foreach (var item in childNode)
{
XmlElement xel = xmlDocument.CreateElement(item.Key);
xel.InnerText = item.Value;
xelKey.AppendChild(xel);
root.AppendChild(xelKey);
}
xmlDocument.Save(XmlPath);
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 删除节点
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public string DeleteXmlNode(string text = null)
{
try
{
bool bl = false;
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(XmlPath);
XmlNode root = xmlDocument.SelectSingleNode("CustomGroupList");
XmlNodeList child = root.ChildNodes;
foreach (XmlNode xn1 in child)
{
XmlElement xe = (XmlElement)xn1;
if (xe.GetAttribute("Text").IndexOf(text.Trim()) > -1)
{
root.RemoveChild(xn1);
bl = true;
}
}
xmlDocument.Save(XmlPath);
if (bl)
{
return "success";
}
else {
return "XML里找不到节点("+ text + ")";
}
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 增加或移除子节点
/// </summary>
/// <param name="text"></param>
/// <param name="childNode"></param>
/// <returns></returns>
public string UpdateXmlNodes(string text, Dictionary<string, string> childNode,bool IsDelete)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(XmlPath);
XmlNode root = xmlDocument.SelectSingleNode("CustomGroupList");
XmlNodeList child = root.ChildNodes;
foreach (XmlNode xn1 in child)
{
XmlElement xe = (XmlElement)xn1;
if (xe.GetAttribute("Text").IndexOf(text.Trim()) > -1)
{
XmlNodeList child1 = xn1.ChildNodes;
if (IsDelete)
{
for (int i = 0; i < child1.Count; i++)
{
if (childNode.ContainsValue(child1.Item(i).InnerText))
{
xn1.RemoveChild(child1.Item(i));
i = 0;
}
}
}
else
{
foreach (var item in childNode)
{
XmlElement xel = xmlDocument.CreateElement(item.Key);
xel.InnerText = item.Value;
xe.AppendChild(xel);
}
}
}
}
xmlDocument.Save(XmlPath);
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}