using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using System.IO;
|
using System.Xml;
|
using HotelPms.Share.IO;
|
using HotelPms.Share.Util;
|
|
namespace HotelPms.Share.Xml
|
{
|
public class SysXmlMgr :IDisposable
|
{
|
/// ****************************** Description *******************************
|
/// VXe¼Ì
|
/// @ÄpNX
|
/// Tv
|
/// @XMLÌìFÇÁAC³AíAõÈÇ
|
/// <>gpû@
|
/// ð
|
/// @2007/11/10 ¬Ø@´ VKì¬
|
/// ****************************** Declarations ******************************
|
#region @Declartions@
|
|
private string fileName;
|
private Dictionary<string, string>? fileInfo;
|
|
#endregion
|
|
#region @Property@
|
|
public string FileName
|
{
|
get { return fileName; }
|
set { fileName = value; }
|
}
|
|
public Dictionary<string, string> FileInfo
|
{
|
get { return fileInfo; }
|
set { fileInfo = value; }
|
}
|
|
#endregion
|
|
#region @Class Event@
|
|
public SysXmlMgr()
|
: this("")
|
{
|
}
|
|
public SysXmlMgr(string fileName)
|
{
|
if (!fileName.Equals(""))
|
{
|
this.fileName = fileName;
|
}
|
if (!File.Exists(this.fileName))
|
{
|
fileInfo = new Dictionary<string, string>();
|
}
|
else
|
{
|
fileInfo = GetNormalXml(this.fileName);
|
}
|
|
}
|
|
#endregion
|
|
#region @Private Function@
|
#endregion
|
|
#region @Public Function@
|
|
public static Dictionary<string, string> GetNormalXml(object hvConfigXml)
|
{
|
Dictionary<string, string> config = new Dictionary<string, string>();
|
XmlDocument doc = new XmlDocument();
|
string itemName = string.Empty;
|
string itemValue = string.Empty;
|
try
|
{
|
if (hvConfigXml is Stream)
|
{
|
doc.Load((hvConfigXml as Stream));
|
}
|
else
|
{
|
doc.Load((hvConfigXml as string));
|
}
|
XmlNode rootnode = doc.DocumentElement;
|
foreach (XmlNode child in rootnode.ChildNodes)
|
{
|
if (child.NodeType == XmlNodeType.Element)
|
{
|
if (child.HasChildNodes)
|
{
|
foreach (XmlNode woItem in child.ChildNodes)
|
{
|
if (woItem.NodeType == XmlNodeType.Element)
|
{
|
if (woItem.Name == "name")
|
{
|
itemName = woItem.InnerText;
|
}
|
else if (woItem.Name == "value")
|
{
|
itemValue = woItem.InnerText;
|
}
|
}
|
}
|
config.Add(itemName, itemValue);
|
}
|
}
|
}
|
return config;
|
}
|
catch
|
{
|
return null;
|
}
|
}
|
|
public int GetInt(string name)
|
{
|
return CConvert.ToInt(GetItem(name));
|
}
|
|
public bool GetBool(string name)
|
{
|
return CConvert.ToBool(GetItem(name));
|
}
|
|
public string GetItem(string name)
|
{
|
if (fileInfo.ContainsKey(name))
|
{
|
return fileInfo[name];
|
}
|
else
|
{
|
return string.Empty;
|
}
|
}
|
|
public bool SetItem(string name, string value)
|
{
|
return SetItem(name, value, false);
|
}
|
|
public bool SetItem(string name, string value, bool forceAdd)
|
{
|
if (fileInfo.ContainsKey(name))
|
{
|
fileInfo[name] = value;
|
return true;
|
}
|
else
|
{
|
if (forceAdd)
|
{
|
Add(name, value);
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
}
|
|
public void Remove(string name)
|
{
|
fileInfo.Remove(name);
|
}
|
|
public void Add(string name,string value)
|
{
|
fileInfo.Add(name, value);
|
}
|
|
/// <summary>
|
/// Û¶·é
|
/// </summary>
|
/// <returns></returns>
|
public bool Save()
|
{
|
try
|
{
|
StringBuilder fileBuilder = new StringBuilder();
|
fileBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
fileBuilder.AppendLine("<environment>");
|
foreach (string item in fileInfo.Keys)
|
{
|
fileBuilder.AppendLine(" <param>");
|
fileBuilder.AppendLine(" <name>" + item + "</name>");
|
fileBuilder.AppendLine(" <value>" + fileInfo[item] + "</value>");
|
fileBuilder.AppendLine(" </param>");
|
}
|
fileBuilder.AppendLine("</environment>");
|
FileOperation.Delete(fileName);
|
File.WriteAllText(fileName, fileBuilder.ToString(), Encoding.UTF8);
|
return true;
|
}
|
catch
|
{
|
return false;
|
}
|
}
|
|
public void ReLoad()
|
{
|
this.fileInfo = GetNormalXml(this.fileName);
|
}
|
|
public void Dispose()
|
{
|
}
|
|
#endregion
|
}
|
}
|