• c# 读写xml 操作类


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;

    namespace KeyCalc
    {
        class XmlClass
        {
            /// <summary>
            /// 配置文件路径
            /// </summary>
            private string xmlFilePath; 
            /// <summary>  
            /// 皮肤索引   
            /// </summary>  
            public string skinIndex; 
            /// <summary>  
            /// 皮肤路径  
            /// </summary>  
            public string skinPath; 
     
            public XmlClass() 
            { 
                //指定XML文件名  
                xmlFilePath = "config.xml"; 
                //检测XML配置文件是否存在  
                if (System.IO.File.Exists(xmlFilePath)) 
                    return; 
                CreateDefaultXml(); 
            }
     
            #region " ReadXML() 读取XML配置文件的参数设置,获取下载的TXT文件路径与上传的数据文件路径"  
            /// <summary>  
            /// 读取XML配置文件的参数设置,获取下载的TXT文件路径与上传的数据文件路径  
            /// </summary>  
            /// <returns></returns>  
            public bool ReadXML() 
            { 
                try 
                { 
                    XmlDocument xmlDoc=new XmlDocument(); 
                    //读取XML配置文件  
                    xmlDoc.Load(xmlFilePath); 
     
                    //读取XML文件节点  
                    XmlNode rootNode = xmlDoc.SelectSingleNode("Skin").SelectSingleNode("ParameterSet"); 
                    if ( rootNode==null ) 
                        throw( new Exception("XML配置文件信息异常")); 
     
                    //获取XML文件参数设置下的节点值  
                    XmlElement downfile = (XmlElement)(rootNode.SelectSingleNode("SkinIndex")); 
                    if (downfile == null) 
                        throw (new Exception("XML配置文件信息异常")); 
                    skinIndex = downfile.InnerText; 
                    XmlElement uploadfile = (XmlElement)(rootNode.SelectSingleNode("SkinPath")); 
                    if (uploadfile == null) 
                        throw (new Exception("XML配置文件信息异常")); 
                    skinPath = uploadfile.InnerText; 
     
                    return true; 
                } 
                catch (System.Exception e) 
                { 
                    throw (e); 
                } 
            } 
            #endregion 
     
     
            #region " WriteXML() 写XML配置文件的参数设置,保存下载的TXT文件路径与上传的数据文件路径"  
            /// <summary>  
            /// 写XML配置文件的参数设置,保存下载的TXT文件路径与上传的数据文件路径  
            /// </summary>  
            /// <returns></returns>  
            public bool WriteXML() 
            { 
                try 
                { 
                    XmlDocument xmlDoc = new XmlDocument(); 
                    //读取XML配置文件  
                    xmlDoc.Load(xmlFilePath); 
     
                    //读取XML文件节点  
                    XmlNode rootNode = xmlDoc.SelectSingleNode("Skin").SelectSingleNode("ParameterSet"); 
                    if (rootNode == null) 
                        throw (new Exception("XML配置文件信息异常")); 
                     
                    //设置XML文件节点的值  
                    XmlElement skinIndexs = (XmlElement)(rootNode.SelectSingleNode("SkinIndex"));
                    if (skinIndexs == null) 
                        throw (new Exception("XML配置文件信息异常"));
                    skinIndexs.InnerText = skinIndex;
                    XmlElement skinPaths = (XmlElement)(rootNode.SelectSingleNode("SkinPath"));
                    if (skinPaths == null) 
                        throw (new Exception("XML配置文件信息异常"));
                    skinPaths.InnerText = skinPath; 
     
                    //保存XML文件  
                    xmlDoc.Save(xmlFilePath); 
                     
                    return true; 
                } 
                catch (System.Exception ex) 
                { 
                    throw (ex); 
                } 
            } 
            #endregion 
     
            #region " CreateDefaultXml() 创建一个默认的XML配置文件"  
            /// <summary>  
            /// 创建一个默认的XML配置文件  
            /// </summary>  
            private void CreateDefaultXml() 
            { 
                try 
                { 
                    XmlDocument xmlDoc = new XmlDocument(); 
                    //创建XML文件描述  
                    XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null); 
                    xmlDoc.AppendChild(dec); 
                    //创建根元素  
                    XmlNode root = xmlDoc.CreateNode(XmlNodeType.Element, "Skin", ""); 
                    xmlDoc.AppendChild(root); 
     
                    //添加参数设置节点  
                    XmlNode parConfig = xmlDoc.CreateNode(XmlNodeType.Element, "ParameterSet", ""); 
                    root.AppendChild(parConfig); 
     
                    //添加下载到PDA的TXT文件路径  
                    XmlElement skinIndexs = xmlDoc.CreateElement("SkinIndex");
                    skinIndexs.InnerText = "";
                    parConfig.AppendChild(skinIndexs); 
                    //添加PDA数据文件上传到PC端的文件路径  
                    XmlElement skinPaths = xmlDoc.CreateElement("SkinPath");
                    skinPaths.InnerText = "";
                    parConfig.AppendChild(skinPaths); 
     
                    //保存xml文件  
                    xmlDoc.Save(xmlFilePath);  
                } 
                catch (System.Exception ex) 
                { 
                    throw ( new Exception("创建默认XML文件失败"+ex.Message)); 
                } 
            } 
            #endregion  

        }
    }

  • 相关阅读:
    C 数组初始化
    Linux函数之snprintf()[一]
    出现一下错误
    IOS通过post方式发送图片续
    IOS通过post方式发送图片
    TCP和UDP的区别趣解
    [转]Release mode debugging with VC++
    [转]Math For Programmers
    OS:kernel and shell
    Reminder: 8020 rule
  • 原文地址:https://www.cnblogs.com/xsmhero/p/2471023.html
Copyright © 2020-2023  润新知