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;
        }
    }

posted on 2008-01-11 10:00  感動常在  阅读(2038)  评论(0编辑  收藏  举报