今天写了个小代码用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();
}
}
}
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();
}
}
}