XML代替INI文件

今天写了个小代码用XML文件代替INI文件,用的是C#,只实现了基本功能,如果你是高手就不用看了,因为我是个初学者,如果对你有用,可以自己完善一下.

 

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

namespace  Kltscb.AppSet
{
    
class Appset
    
{
        
/// <summary>
        
/// 配置文件
        
/// </summary>

        private string _FilePath;
        
/// <summary>
        
/// 是否有配置文件
        
/// </summary>

        private bool _HasFile;
        
private XmlDocument _Doc;
        
public Appset()
        
{
            _Doc 
= new XmlDocument();
            
//throw new System.NotImplementedException();
        }


        
/// <summary>
        
/// 配置文件是否存在
        
/// </summary>

        public bool FileExist
        
{
            
get
            
{
                
return _HasFile;
            }

        }


        
/// <summary>
        
/// 文件路径
        
/// </summary>

        public string FilePath
        
{
            
get
            
{
                
return _FilePath;
            }

            
set
            
{
                _FilePath 
= value;
            }

        }


        
/// <summary>
        
/// 添加一个配置
        
/// </summary>
        
/// <param name="Root">节点名字</param>
        
/// <param name="Name">属性名字</param>
        
/// <param name="Value">属性值</param>

        private bool AddValue(string Root, string Name, string Value)
        
{
            
if (_Doc != null)
            
{
                
//查找根节点
                XmlNode RootNode = _Doc.SelectSingleNode("Root");
                
if (RootNode != null)//如果存在根节点
                {
                    
//查找是否已有该节点
                    XmlNode Node = RootNode.SelectSingleNode(Root);
                    
if (Node != null)//如果有该节点
                    {
                        
//创造这个配置属性
                        XmlNode HasName = Node.SelectSingleNode(Name);
                        
//如果已有该属性
                        if (HasName==null)
                        
{
                            XmlElement ThisNode 
= _Doc.CreateElement(Name);
                            ThisNode.InnerText 
= Value;
                            Node.AppendChild(ThisNode);
                            
return true;
                        }

                        
else
                            
return false;
                    }

                    
else
                        
return false;
                }

                
else
                
return false;
            }

            
else
                
return false;
            
//throw new System.NotImplementedException();
        }


        
/// <summary>
        
/// 设置一个配置
        
/// </summary>
        
/// <param name="Root">节点名字</param>
        
/// <param name="Name">属性名字</param>
        
/// <param name="Value">属性值</param>

        private bool SetValue(string Root, string Name, string Value)
        
{
            
if (_Doc != null)
            
{
                
//查找根节点
                XmlNode RootNode = _Doc.SelectSingleNode("Root");
                
if (RootNode != null)//如果存在根节点
                {
                    
//查找是否已有该节点
                    XmlNode Node = RootNode.SelectSingleNode(Root);
                    
if (Node != null)//如果有该节点
                    {
                        
//创造这个配置属性
                        XmlNode HasName = Node.SelectSingleNode(Name);
                        
//如果已有该属性
                        if (HasName != null)
                        
{
                            HasName.InnerText 
= Value;
                            
return true;
                        }

                        
else
                            
return false;
                    }

                    
else
                        
return false;
                }

                
else
                    
return false;
            }

            
else
                
return false;
            
//throw new System.NotImplementedException();
        }


        
/// <summary>
        
/// 取得一个配置
        
/// </summary>
        
/// <param name="Root">节点名字</param>
        
/// <param name="Name">属性名字</param>
        
/// <param name="Value">属性值</param>

        private string GetValue(string Root, string Name)
        
{
            
if (_Doc != null)
            
{
                
//查找根节点
                XmlNode RootNode = _Doc.SelectSingleNode("Root");
                
if (RootNode != null)//如果存在根节点
                {
                    
//查找是否已有该节点
                    XmlNode Node = RootNode.SelectSingleNode(Root);
                    
if (Node != null)//如果有该节点
                    {
                        
//创造这个配置属性
                        XmlNode HasName = Node.SelectSingleNode(Name);
                        
//如果已有该属性
                        if (HasName != null)
                        
{
                            
return HasName.InnerText;
                        }

                        
else
                            
return "";
                    }

                    
else
                        
return "";
                }

                
else
                    
return "";
            }

            
else
                
return "";
            
//throw new System.NotImplementedException();
        }


        
/// <summary>
        
/// 添加一个节点
        
/// </summary>
        
/// <param name="Root">节点名字</param>

        private bool AddNode(string Root)
        
{
            
//是否有配置文档
            if (_Doc!=null)
            
{
                
//查找根节点
                XmlNode RootNode = _Doc.SelectSingleNode("Root");
                
if (RootNode!=null)//如果存在根节点
                {
                    
//查找是否已有该节点
                    if (RootNode.SelectSingleNode(Root) == null)//如果没有该节点
                    {
                        
//创造这个节点
                        XmlElement Node = _Doc.CreateElement("", Root, "");
                        RootNode.AppendChild(Node);
                        
return true;
                    }

                    
else
                        
return false;
                }

                
else
                    
return false;
                
            }

            
else
                
return false;
            
//throw new System.NotImplementedException();
        }


        
private bool CreateAppSet()
        
{
            
//实例一个XML
            
//_Doc = new XmlDocument();
            
//_Doc.Load(FilePath);
            
//添加声明
            XmlNode Node = _Doc.CreateNode(XmlNodeType.XmlDeclaration, """");
            _Doc.AppendChild(Node);
            
//添加根节点
            XmlElement Root = _Doc.CreateElement("""Root""");
            _Doc.AppendChild(Root);
            
return true;

            
            
//throw new System.NotImplementedException();
        }


        
private bool LoadAppSet(string FilePath)
        
{
            
if (File.Exists(FilePath))
            
{
                _HasFile 
= true;
                _FilePath 
= FilePath;
                _Doc.Load(FilePath);
                
return true;
            }

            
else
            
{
                _HasFile 
= false;
                
return false;
            }

            
//throw new System.NotImplementedException();
        }


        
private bool SaveAppSet()
        
{
            
try
            
{
                _Doc.Save(_FilePath);
                
return true;

            }

            
catch (Exception)
            
{

                
return false;
            }

            
            
//throw new System.NotImplementedException();
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值