• XML操作类


    public class XmlControl:Object
     {
            protected string strXmlFile;

            protected XmlDocument objXmlDoc = new XmlDocument();

            public XmlControl(string XmlFile, Boolean bOverWrite, string sRoot)
            {
                try
                {
                    //如果覆盖模式,则强行创建一个xml文档
                    if (bOverWrite)
                    {
                        objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "gb2312", null));//设置xml的版本,格式信息
                        objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));//创建根元素
                        objXmlDoc.Save(XmlFile);//保存
                    }
                    else //否则,检查文件是否存在,不存在则创建
                    {
                        if (!(File.Exists(XmlFile)))
                        {
                            objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "gb2312", null));
                            objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));
                            objXmlDoc.Save(XmlFile);
                        }
                    }
                    objXmlDoc.Load(XmlFile);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                strXmlFile = XmlFile;
            }

            public DataView GetData(string XmlPathNode)
            {
                //查找数据。返回一个DataView
                DataSet ds = new DataSet();
                try
                {
                    StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
                    ds.ReadXml(read);
                    return ds.Tables[0].DefaultView;
                }
                catch
                {
                    return null;
                }

            }

            public void UpdateNode(string xmlPathNode, string content)
            {
                //更新节点內容,如果多个相同的节点,则更新第一个节点
                objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
            }

            public void UpdateNode(string xmlPathNode, string[] arrAttribName, string[] arrAttribContent, string content)
            {
                //更新节点,带属性和内容


                //修改内容
                objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;

                //修改属性
                for (int i = 0; i <= arrAttribName.GetUpperBound(0); i++)
                {
                    objXmlDoc.SelectSingleNode(xmlPathNode).Attributes[i].Value = arrAttribContent[i];
                }
            }

            public void DeleteNode(string Node)
            {
                //刪除一个节点。
                try
                {
                    string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
                    objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
                }
                catch {
                    //throw;  
                    return;
                }
            }

            public void InsertNode(string mainNode, string ChildNode, string Element, string Content)
            {
                //插入一节点和此节点的一子节点。
                XmlNode objRootNode = objXmlDoc.SelectSingleNode(mainNode);
                XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
                objRootNode.AppendChild(objChildNode);//插入节点
                XmlElement objElement = objXmlDoc.CreateElement(Element);
                objElement.InnerText = Content;
                objChildNode.AppendChild(objElement);//插入子节点
            }

            public void InsertNode(string mainNode, string Element, string Attrib, string AttribContent, string Content)
            {
                //插入一个节点,带一属性。
                XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
                XmlElement objElement = objXmlDoc.CreateElement(Element);
                objElement.SetAttribute(Attrib, AttribContent);
                objElement.InnerText = Content;
                objNode.AppendChild(objElement);
            }

            public void InsertNode(string mainNode, string Element, string Content)
            {
                //插入一个节点,不带属性。
                XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
                XmlElement objElement = objXmlDoc.CreateElement(Element);
                objElement.InnerText = Content;
                objNode.AppendChild(objElement);
            }

            public void InsertNode(string mainNode, string elementName, string[] arrAttributeName, string[] arrAttributeContent, string elementContent)
            {
                XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
                XmlElement objElement = objXmlDoc.CreateElement(elementName);
                for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
                {
                    objElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
                }
                objElement.InnerText = elementContent;
                objNode.AppendChild(objElement);
            }

            public void Save()
            {
                //保存文档。
                try
                {
                    objXmlDoc.Save(strXmlFile);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                objXmlDoc = null;
            }
        }

  • 相关阅读:
    Extjs4.x Ext.tree.Panel基本树控件的使用案例、源码
    Extjs4 autoload和iframe的取舍
    Extjs4 文件目录结构
    IIS7.5 配置MVC3.0 伪静态,不能访问html伪静态页面
    DB2存储过程模版
    C#后台调用跨域MVC服务,带Cookie验证
    基于SenchaTouch项目案例
    EF Code First DataAnnotations
    找不到方法:“Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)”。
    Babylon 3D engine (Silverlight)
  • 原文地址:https://www.cnblogs.com/wang123/p/1034738.html
Copyright © 2020-2023  润新知