zoukankan      html  css  js  c++  java
  • XmlHelper

    using System;
        using System.Collections.Generic;
        using System.Data;
        using System.IO;
        using System.Xml;
        using System.Xml.XPath;

     public class XmlHelper
        {
            private XPathNavigator nav;
            private XmlNode node;
            private XPathNodeIterator NodeIter;
            protected XmlDocument objXmlDoc = new XmlDocument();
            private string strExpression;
            protected string strXmlFile;
    
       public XmlHelper(string XmlFile)
        {
            try
              {
                 this.objXmlDoc.Load(XmlFile);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                this.strXmlFile = XmlFile;
            }
        public void Delete(string Node)
            {
                string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
                this.objXmlDoc.SelectSingleNode(mainNode).RemoveChild(this.objXmlDoc.SelectSingleNode(Node));
            }
    
            public void DeleteChildren(string Node)
            {
                this.objXmlDoc.SelectSingleNode(Node).RemoveAll();
            }

       public DataView GetData(string XmlPathNode)
            {
                DataSet ds = new DataSet();
                this.nav = this.objXmlDoc.CreateNavigator();
                this.nav = this.nav.SelectSingleNode(XmlPathNode);
                if (this.nav == null)
                {
                    throw new Exception("没有找到结点");
                }
                StringReader read = new StringReader(this.nav.get_OuterXml());
                ds.ReadXml(read);
                if (ds.Tables.Count > 0)
                {
                    return ds.Tables[0].DefaultView;
                }
                return null;
            }
      public string GetElementValue(string MainNode)
            {
                this.nav = this.objXmlDoc.CreateNavigator();
                this.strExpression = string.Format("{0}", MainNode);
                this.nav = this.nav.SelectSingleNode(this.strExpression);
                return this.nav.get_Value();
            }
    
            public string GetElementValue(string MainNode, string Element)
            {
                this.nav = this.objXmlDoc.CreateNavigator();
                this.strExpression = string.Format("{0}/{1}", MainNode, Element);
                this.nav = this.nav.SelectSingleNode(this.strExpression);
                return this.nav.get_Value();
            }
      public string GetNodeAttrValue(string MainNode, string Attr)
            {
                this.nav = this.objXmlDoc.CreateNavigator();
                this.strExpression = string.Format("{0}@{1}", MainNode, Attr);
                this.nav.SelectSingleNode(this.strExpression);
                return this.nav.get_Value();
            }
    
            public List<string> GetSubNodes(string MainNode)
            {
                List<string> lst = new List<string>();
                this.nav = this.objXmlDoc.CreateNavigator();
                this.nav = this.nav.SelectSingleNode(MainNode);
                this.NodeIter = this.nav.SelectChildren(XPathNodeType.Element);
                foreach (XPathNavigator n in this.NodeIter)
                {
                    lst.Add(n.get_Value());
                }
                return lst;
            }

      public void InsertElement(string MainNode, string Element, string Content)
            {
                XmlNode objNode = this.objXmlDoc.SelectSingleNode(MainNode);
                XmlElement objElement = this.objXmlDoc.CreateElement(Element);
                objElement.InnerText = Content;
                objNode.AppendChild(objElement);
            }
    
            public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content)
            {
                XmlNode objNode = this.objXmlDoc.SelectSingleNode(MainNode);
                XmlElement objElement = this.objXmlDoc.CreateElement(Element);
                objElement.SetAttribute(Attrib, AttribContent);
                objElement.InnerText = Content;
                objNode.AppendChild(objElement);
            }
    
            public void InsertNode(string MainNode, string ChildNode, string Element, string Content)
            {
                XmlNode objRootNode = this.objXmlDoc.SelectSingleNode(MainNode);
                XmlElement objChildNode = this.objXmlDoc.CreateElement(ChildNode);
                objRootNode.AppendChild(objChildNode);
                XmlElement objElement = this.objXmlDoc.CreateElement(Element);
                objElement.InnerText = Content;
                objChildNode.AppendChild(objElement);
            }
      public bool IsExists(string Node)
            {
                try
                {
                    if (this.objXmlDoc.SelectSingleNode(Node) == null)
                    {
                        return false;
                    }
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
       public void RemoveAll(string Node)
            {
                XmlNodeList xnl = this.objXmlDoc.SelectNodes(Node);
                string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
                foreach (XmlNode xn in xnl)
                {
                    xn.RemoveAll();
                }
            }
    
            public void Replace(string XmlPathNode, string Content)
            {
                this.objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
            }
    
            public void Save()
            {
                try
                {
                    this.objXmlDoc.Save(this.strXmlFile);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                this.objXmlDoc = null;
            }
        }
    }
    
  • 相关阅读:
    Spring Boot开发Web应用
    使用阿里云Docker镜像加速
    六种微服务架构的设计模式
    HashMap按键排序和按值排序
    Docker搭建本地私有仓库
    Ubuntu 14.04主机上部署k8s集群
    Ubuntu 16.04下搭建kubernetes集群环境
    Docker中images中none的镜像删除
    docker 下 alpine 镜像设置时区的有效办法
    offsetLeft和style.left的区别
  • 原文地址:https://www.cnblogs.com/zengxiangzhan/p/1651836.html
Copyright © 2011-2022 走看看