国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

XML操作

2019-11-17 01:37:21
字體:
供稿:網(wǎng)友

xml操作

public class XMLOperation { PRivate static readonly ILog log = LogManager.GetLogger(typeof(mainForm)); Message mesg = null; XmlDocument doc; public XMLOperation(string fileName) { mesg = new Message(); try { doc = new XmlDocument(); doc.Load(fileName); } catch (Exception ex) { Tools.LogMessage(log, mesg, ex.Message); } } public XMLOperation() { mesg = new Message(); try { doc = new XmlDocument(); } catch (Exception ex) { Tools.LogMessage(log, mesg, ex.Message); } }

public void CreateRootNode(string name) { try { if (name == "" || name.Trim().Length == 0) { return; }

XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); XmlNode node = doc.CreateNode(XmlNodeType.Element, name, ""); doc.AppendChild(node); } catch (Exception ex) { Tools.LogMessage(log, mesg, ex.Message); } }

public XmlNode AddNode(string pNodeName,string nodeName) { if (nodeName == null || nodeName.Length == 0) { return null; } XmlElement xe = null;

try { XmlNode nd = doc.SelectSingleNode(pNodeName); xe = doc.CreateElement(nodeName); nd.AppendChild(xe); } catch (Exception ex) { Tools.LogMessage(log, mesg, ex.Message); } return xe;

}

public void AddElement(XmlNode pNode, string elmName,string value) { try { XmlElement ele = doc.CreateElement(elmName); ele.InnerText = value; pNode.AppendChild(ele); } catch (Exception ex) { Tools.LogMessage(log, mesg, ex.Message); } }

public void SaveDoc(string fileName) { doc.Save(fileName); }

public void ClearNodes(string rootNodeName) { try { XmlNode root = doc.SelectSingleNode(rootNodeName); if (root != null) { root.RemoveAll(); } } catch (Exception ex) { Tools.LogMessage(log, mesg, ex.Message); } }

public Dictionary<string, string> GetKeyValues( string rootNodeName,string elmentKeyName,string elmentKeyValue) { Dictionary<string, string> dicRet = new Dictionary<string, string>(); try { XmlNode rootNode = doc.SelectSingleNode(rootNodeName); if (rootNode.HasChildNodes) { XmlNodeList nodeList = rootNode.ChildNodes; foreach (XmlNode node in nodeList) { if (node.HasChildNodes) { XmlNodeList subNodeList = node.ChildNodes; bool bFind = false; foreach (XmlNode subNode in subNodeList) { if (subNode.Name == elmentKeyName && subNode.InnerText == elmentKeyValue) { bFind = true; dicRet.Add(subNode.Name, subNode.InnerText); continue; } if (bFind) { dicRet.Add(subNode.Name, subNode.InnerText); } } if (bFind) { break; } } } } } catch (Exception ex) { Tools.LogMessage(log, mesg, ex.Message); }

return dicRet; } public List<string> GetValues(string rootNodeName, string elmentKeyName) { List<string> listRet =new List<string>(); try { XmlNode rootNode = doc.SelectSingleNode(rootNodeName); if (rootNode.HasChildNodes) { XmlNodeList nodeList = rootNode.ChildNodes; foreach (XmlNode node in nodeList) { if (node.HasChildNodes) { XmlNodeList subNodeList = node.ChildNodes; foreach (XmlNode subNode in subNodeList) { if (subNode.Name == elmentKeyName) { listRet.Add(subNode.InnerText); break; } } } } } } catch (Exception ex) { Tools.LogMessage(log, mesg, ex.Message); } return listRet; }

public Dictionary<string, string> GetKeyValuesEx(string rootNodeName, string atrributeName) { Dictionary<string, string> dicRet = new Dictionary<string, string>();

try { XmlNode rootNode = doc.SelectSingleNode(rootNodeName); if (rootNode.HasChildNodes) { XmlNodeList nodeList = rootNode.ChildNodes; foreach (XmlNode node in nodeList) { XmlElement xe = (XmlElement)node; string key = xe.GetAttribute(atrributeName); if (!dicRet.ContainsKey(key)) { dicRet.Add(key, xe.InnerText); } } } } catch (Exception ex) { Tools.LogMessage(log, mesg, ex.Message); }

return dicRet; } public void RemoveNode(string rootNodeName, string keyName,string value) {

XmlNode rootNode = doc.SelectSingleNode(rootNodeName); if (rootNode.HasChildNodes) { XmlNodeList nodeList = rootNode.ChildNodes; foreach (XmlNode node in nodeList) { if (node.HasChildNodes) { XmlNodeList subNodeList = node.ChildNodes; foreach (XmlNode subNode in subNodeList) { if (subNode.Name == keyName) { if (subNode.InnerText == value) { rootNode.RemoveChild(subNode.ParentNode); break; } } } } } } } public List<List<string>> GetAllKeyValues(string rootNodeName) { List<List<string>> listRet = new List<List<string>>(); XmlNode rootNode = doc.SelectSingleNode(rootNodeName); if (rootNode.HasChildNodes) { XmlNodeList nodeList = rootNode.ChildNodes; foreach (XmlNode node in nodeList) { if (node.HasChildNodes) { List<string> list = new List<string>(); XmlNodeList subNodeList = node.ChildNodes; foreach (XmlNode subNode in subNodeList) { list.Add(subNode.InnerText); } listRet.Add(list); } } }

return listRet; } }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 太康县| 云龙县| 合山市| 武强县| 防城港市| 西乌珠穆沁旗| 杭锦后旗| 佛教| 河南省| 怀宁县| 太谷县| 厦门市| 长子县| 林周县| 会理县| 绥阳县| 西平县| 陕西省| 二连浩特市| 九台市| 临潭县| 信宜市| 万载县| 湘西| 青神县| 海淀区| 建宁县| 诸城市| 黔江区| 紫阳县| 侯马市| 乌兰浩特市| 朝阳市| 宁蒗| 吴堡县| 霍山县| 百色市| 裕民县| 河曲县| 全椒县| 长乐市|