using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using XRTool.Util;
///
/// 一个xml编辑器
///
namespace PublicTools.XMLDataBase
{
///
/// 针对某个xml文件进行编辑的类
///
public class XMLDataEditor : TableInterface
{
private XmlDocument xmlDoc; //可操作的xml文档
private XmlNode xmlRootNode; //根节点
///
/// 文件的绝对路径
///
private string filePath;
///
/// 表格的名称
///
private string tableName;
///
/// 根节点的名称
///
///
/// 以绝对路径创建一个对象
///
///
public XMLDataEditor(string filePath)
{
this.filePath = filePath;
}
public XMLDataEditor()
{
}
public bool Open()
{
return Open(filePath);
}
///
/// 打开xml表格
///
///
///
public bool Open(string path)
{
if (xmlDoc != null && !string.IsNullOrEmpty(this.filePath))
{
return true;
}
///开启之前先关闭
//Close();
if (File.Exists(path))
{
xmlDoc = new XmlDocument();
tableName = Path.GetFileNameWithoutExtension(path);
try
{
xmlDoc.Load(path);
xmlRootNode = xmlDoc.SelectSingleNode(tableName);
return xmlRootNode != null;
}
catch (Exception ex)
{
UnityLog.LogError(path + ex.ToString());
}
}
return false;
}
public void Close()
{
if (xmlDoc != null && !string.IsNullOrEmpty(filePath))
{
try
{
xmlDoc.Save(filePath);
xmlDoc = null;
//filePath = null;
}
catch (Exception ex)
{
UnityLog.LogError(filePath + "\n" + ex.ToString());
}
}
}
public bool InsertData(T data, string priKey)
{
Dictionary field = ClazzFactory.ClazzAnalyze(data);
if (field != null && field.Count > 0)
{
XmlNode table = xmlRootNode;
if (string.IsNullOrEmpty(priKey))
{
foreach (var item in field)
{
priKey = item.Value;
break;
}
}
return InsertData(data, table, field, priKey);
}
return false;
}
///
/// 插入数据
///
///
///
///
private bool InsertData(T data, XmlNode table, Dictionary field, string priKey)
{
try
{
if (table.SelectSingleNode(priKey) != null)
{
return UpdateData(data, table, field, priKey);
}
///添加一条数据
///按照key值添加一个节点元素
XmlElement element = xmlDoc.CreateElement(priKey);
///循环遍历变量集合,将类的数据集合起来添加
foreach (var item in field)
{
XmlAttribute xa = xmlDoc.CreateAttribute(item.Key);
xa.Value = item.Value;
element.Attributes.Append(xa);
}
table.AppendChild(element);
return true;
}
catch (Exception ex)
{
UnityLog.LogError(data + " " + priKey + ex.ToString());
Close();
}
return false;
}
public bool InsertData(T data)
{
return InsertData(data, null);
}
///
/// 尝试获取xml的一个表,如果此xml没有此表,则创建一个
///
///
///
public XmlNode TryGetTable(string tableName)
{
XmlNode node = xmlRootNode.SelectSingleNode(tableName);
if (node != null)
{
return node;
}
return CreateTable(tableName);
}
///
/// 创建一个子节点
///
///
public XmlNode CreateTable(string tableName)
{
XmlNode table = xmlDoc.CreateElement(tableName);
xmlRootNode.AppendChild(table);
Save();
return table;
}
///
///
///
///
///
///
public T FindData(string priKey)
{
T t = default(T);
XmlNode table = xmlRootNode;
try
{
XmlNode obj = table.SelectSingleNode(priKey);
if (obj != null)
{
Dictionary dic = new Dictionary();
for (int i = 0; i < obj.Attributes.Count; i++)
{
dic.Add(obj.Attributes[i].LocalName, obj.Attributes[i].Value);
}
t = ClazzFactory.ClazzPack(dic);
}
}
catch (Exception ex)
{
Close();
UnityLog.LogError(priKey + ex.ToString());
}
return t;
}
public List FindAllData()
{
XmlNode table = xmlRootNode;
if (table == null || table.ChildNodes.Count == 0)
{
return null;
}
List list = new List();
for (int i = 0; i < table.ChildNodes.Count; i++)
{//遍历表的子节点
Dictionary values = new Dictionary();
for (int j = 0; j < table.ChildNodes[i].Attributes.Count; j++)
{
values.Add(table.ChildNodes[i].Attributes[j].LocalName, table.ChildNodes[i].Attributes[j].Value);
}
list.Add(ClazzFactory.ClazzPack(values));
}
return list;
}
public List FindAllData(string data)
{
return null;
}
///
/// 查找所有对象
///
///
///
public List FindAllData(byte[] data)
{
try
{
xmlDoc = new XmlDocument();
string xmlStr = UnityUtil.GetUTF8String(data);
xmlDoc.LoadXml(xmlStr);
tableName = typeof(T).Name;
xmlRootNode = xmlDoc.SelectSingleNode(tableName);
}
catch (Exception ex)
{
UnityLog.LogError(ex.ToString());
}
return FindAllData();
}
public bool UpdateData(T data, string priKey)
{
Dictionary field = ClazzFactory.ClazzAnalyze(data);
if (field != null && field.Count > 0)
{
XmlNode table = xmlRootNode;
if (string.IsNullOrEmpty(priKey))
{
foreach (var item in field)
{
priKey = item.Value;
break;
}
}
return UpdateData(data, table, field, priKey);
}
return false;
}
private bool UpdateData(T data, XmlNode table, Dictionary field, string priKey)
{
try
{
XmlNode obj = table.SelectSingleNode(priKey);
if (obj == null)
{
//待更新数据不存在,更新失败
return false;
}
///需要判断变量名是否发生了变化
bool isChanged = false;
if (obj.Attributes.Count != field.Count)
{
isChanged = true;
}
if (!isChanged)
{
int index = 0;
foreach (var item in field)
{
///变量名不一致
if (obj.Attributes[index].LocalName != item.Key)
{
isChanged = true;
break;
}
index++;
}
}
if (isChanged)
{
if (DeleteData(priKey))
{
return InsertData(data, priKey);
}
else
{
return false;
}
}
else
{
foreach (var item in field)
{
if (obj.Attributes[item.Key].Value != item.Value)
{
obj.Attributes[item.Key].Value = item.Value;
isChanged = true;
}
}
}
return isChanged;
}
catch (Exception ex)
{
UnityLog.LogError(data + " " + priKey + ex.ToString());
Close();
}
return false;
}
public bool UpdateData(T data)
{
return UpdateData(data, null);
}
public bool DeleteData(string key)
{
XmlNode table = xmlRootNode;
try
{
XmlNode obj = table.SelectSingleNode(key);//按照key值确定元素对象
if (obj == null)
{
return false;
}
obj.RemoveAll();
table.RemoveChild(obj);
return true;
}
catch (Exception ex)
{
UnityLog.LogError(ex.ToString());
}
return false;
}
public bool DeleteAllData()
{
try
{
XmlNode table = xmlRootNode;
table.RemoveAll();
return true;
}
catch (Exception ex)
{
UnityLog.LogError(ex.ToString());
}
return false;
}
///
/// 建表
///
///
///
public bool Create(string tableName)
{
try
{
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
if (xmlDoc == null)
{
xmlDoc = new XmlDocument();
}
this.tableName = Path.GetFileNameWithoutExtension(tableName);
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlRootNode = xmlDoc.CreateElement(this.tableName);
xmlDoc.AppendChild(xmlDeclaration);
xmlDoc.AppendChild(xmlRootNode);
fs.Dispose();
fs.Close();
return xmlRootNode != null;
}
catch (Exception ex)
{
UnityLog.Log("Create Error" + this.tableName + ex.ToString());
}
return false;
}
public void Save()
{
if (xmlDoc != null && !string.IsNullOrEmpty(filePath))
{
try
{
xmlDoc.Save(filePath);
}
catch (Exception ex)
{
UnityLog.LogError(filePath + "\n" + ex.ToString());
}
}
}
}
}