/// <summary>
/// ***************************
/// ****操作XML文件封装类 *****
/// **** V1.0 12-07-27 *****
/// **** 狼鹰组织 *****
/// **** hyxwill *****
/// ***************************
/// </summary>
public sealed class ReadWriteXML
{
public bool QueryFolderIt(string path, string xmlName)
{
try
{
FileInfo fi = new FileInfo(string.Concat(@"", path, xmlName));
return fi.Exists;
}
catch (Exception)
{
return false;
}
}
//是新建还是插入,自动判断
public void ICAutoXML(string path, string xmlName, string value)
{
if (QueryFolderIt(string.Concat(@"", path), xmlName))
{
InsertXML(string.Concat(@"",path), xmlName, value);
}
else
CreateXML(string.Concat(@"", path), xmlName, value);
}
public void CreateXML(string path, string xmlName, string value)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
//建立Xml的定义声明
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
xmlDoc.AppendChild(dec);
//创建根节点
XmlElement root = xmlDoc.CreateElement("BGHelper");
xmlDoc.AppendChild(root);
//创建根节点的子节点Book
XmlNode User = xmlDoc.CreateElement("User");
//创建Book的子节点
XmlElement Name = xmlDoc.CreateElement("Name");
Name.InnerText = value;
User.AppendChild(Name);
//将Book添加到根节点上
root.AppendChild(User);
//保存文档(如果已经存在该文件,则更新之;如果没有,则创建该文件)
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
public void InsertXML(string path, string xmlName, string value)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
XmlNode root = xmlDoc.SelectSingleNode("BGHelper");
XmlElement User = xmlDoc.CreateElement("User");
XmlElement Name = xmlDoc.CreateElement("Name");
Name.InnerText = value;
User.AppendChild(Name);
root.AppendChild(User);
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
public void UpdateXML(string path, string xmlName, string operValue, string newValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
//获取Books节点的所有子节点
XmlNodeList list = xmlDoc.SelectSingleNode("BGHelper").ChildNodes;//User
bool breakAll = false;
//遍历所有子节点
foreach (XmlNode node in list)
{
if (breakAll) break;
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)node;
foreach (XmlNode nd in xe.ChildNodes)
{
XmlElement x = (XmlElement)nd;
if (x.Name == "Name")
{
if (x.InnerText == operValue)
{
x.InnerText = newValue;
breakAll = true;
break;
}
}
}
}
//保存
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//删除
public void DeleteXML(string path, string xmlName, string operValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
//获取Books节点的所有子节点
XmlNodeList list = xmlDoc.SelectSingleNode("BGHelper").ChildNodes;//User
bool breakAll = false;
//遍历所有子节点
foreach (XmlNode node in list)
{
if (breakAll)
break;
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)node;
foreach (XmlNode xn in node.ChildNodes)
{
XmlElement x = (XmlElement)xn;
if (x.Name == "Name")
{
if (x.InnerText == operValue)
{
xe.RemoveAll();
breakAll = true;
break;
}
}
}
}
//保存
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//所有
public string ReadString(string path, string xmlName)
{
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(string.Concat(@"", path, xmlName));
string str = string.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.LocalName.Equals("Name"))
{
str += reader.ReadString() + ",";
}
}
}
if (str != string.Empty && str.Length > 0)
str = str.Substring(0, str.Length - 1);
return (str);
}
catch
{
throw;
}
finally
{
if (reader != null)
{
reader.Close();
reader = null;
}
}
}
//所有
public List<string> ReadStrings(string path, string xmlName)
{
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(string.Concat(@"", path, xmlName));
List<string> str = new List<string>();// string.Empty;
string reads = "";
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.LocalName.Equals("Name"))
{
reads = reader.ReadString();
if (reads != "")
str.Add(reads);
}
}
}
return (str);
}
catch
{
throw;
}
finally
{
if (reader != null)
{
reader.Close();
reader = null;
}
}
}
//所有,自定义,适用于单节点多子节点
public List<Entity.ListEntity> ReadStrings(string path, string xmlName, string[] xmlnodes)
{
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(string.Concat(@"", path, xmlName));
List<Entity.ListEntity> str = new List<Entity.ListEntity>();// string.Empty;
Entity.ListEntity le = null;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
foreach(string nodes in xmlnodes)
if (reader.LocalName.Equals(nodes))
{
le = new Entity.ListEntity();
le.IDName = nodes;
le.LEValue = reader.ReadString();
str.Add(le);//str.Add(reader.ReadString());
}
}
}
return (str);
}
catch
{
//throw;
return null;
}
finally
{
if (reader != null)
{
reader.Close();
reader = null;
}
}
}
public void ICAutoXML(string path, string xmlName,string nodesName, Entity.ListEntity[] value)
{
if (QueryFolderIt(string.Concat(@"", path), xmlName))
{
InsertXML(string.Concat(@"", path),nodesName, xmlName, value);
}
else
CreateXML(string.Concat(@"", path),nodesName, xmlName, value);
}
//删除还是新建
public void DCAutoXML(string path, string xmlName, string nodesName, Entity.ListEntity[] value)
{
if (QueryFolderIt(string.Concat(@"", path), xmlName))
{
new Folder().DeleteFile(string.Concat(@"", path,"//", xmlName));
CreateXML(string.Concat(@"", path), xmlName,nodesName, value);
}
else
CreateXML(string.Concat(@"", path), xmlName,nodesName, value);
}
public void CreateXML(string path, string xmlName, string nodesName, Entity.ListEntity[] value)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
//建立Xml的定义声明
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
xmlDoc.AppendChild(dec);
//创建根节点
XmlElement root = xmlDoc.CreateElement("BGHelper");
xmlDoc.AppendChild(root);
//创建根节点的子节点Book
XmlNode User = xmlDoc.CreateElement(nodesName);
for (int i = 0; i < value.Length; i++)
{
if (value[i].IDName != null&&value[i].LEValue!=null)
{
//创建Book的子节点
XmlElement Name = xmlDoc.CreateElement(value[i].IDName);
Name.InnerText = value[i].LEValue.ToString();
User.AppendChild(Name);
}
}
//将Book添加到根节点上
root.AppendChild(User);
//保存文档(如果已经存在该文件,则更新之;如果没有,则创建该文件)
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
//throw;
}
}
public void InsertXML(string path, string xmlName,string nodesName, Entity.ListEntity[] value)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
XmlNode root = xmlDoc.SelectSingleNode("BGHelper");
XmlElement User = xmlDoc.CreateElement(nodesName);
for (int i = 0; i < value.Length; i++)
{
XmlElement Name = xmlDoc.CreateElement(value[i].IDName);
Name.InnerText = value[i].LEValue.ToString();
User.AppendChild(Name);
}
root.AppendChild(User);
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//适用于单节点多子节点
public void UpdateXML(string path, string xmlName,string nodesName, Entity.ListEntity[] operValue, Entity.ListEntity[] newValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
//获取Books节点的所有子节点
XmlNodeList list = xmlDoc.SelectSingleNode(string.Concat("BGHelper//",nodesName)).ChildNodes;//User
bool breakAll = false;
//遍历所有子节点
foreach (XmlNode node in list)
{
if (breakAll) break;
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)node;
foreach (XmlNode nd in xe.ChildNodes)
{
XmlElement x = (XmlElement)nd;
for (int i = 0; i < operValue.Length; i++)
{
if (x.Name == operValue[i].IDName)
{
x.InnerText = newValue[i].LEValue.ToString();
breakAll = true;
break;
}
}
}
}
//保存
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//删除 适用于单节点多子节点
public void DeleteXML(string path, string xmlName, string nodesName, string operValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
//获取Books节点的所有子节点
XmlNodeList list = xmlDoc.SelectSingleNode(string.Concat("BGHelper//", nodesName)).ChildNodes;//User
bool breakAll = false;
//遍历所有子节点
foreach (XmlNode node in list)
{
if (breakAll)
break;
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)node;
foreach (XmlNode xn in node.ChildNodes)
{
XmlElement x = (XmlElement)xn;
if (x.Name == "Name")
{
xe.RemoveAll();
breakAll = true;
break;
}
}
}
//保存
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//直写 keyName元素名 keyValue值
public void WriteXML(string path, string xmlName, List<string> keyName, List<string> keyValue)
{
try
{
XmlTextWriter writer = new XmlTextWriter(string.Concat(@"", path, xmlName), null);
writer.Formatting = Formatting.Indented;
writer.Indentation = 6;
writer.WriteStartDocument();
writer.WriteStartElement("BGHelper");
writer.WriteStartElement("User");
for (int i = 0; i < keyName.Count; i++)
{
writer.WriteElementString(keyName[i], keyValue[i]);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
catch
{
throw;
}
}
}
/// ***************************
/// ****操作XML文件封装类 *****
/// **** V1.0 12-07-27 *****
/// **** 狼鹰组织 *****
/// **** hyxwill *****
/// ***************************
/// </summary>
public sealed class ReadWriteXML
{
public bool QueryFolderIt(string path, string xmlName)
{
try
{
FileInfo fi = new FileInfo(string.Concat(@"", path, xmlName));
return fi.Exists;
}
catch (Exception)
{
return false;
}
}
//是新建还是插入,自动判断
public void ICAutoXML(string path, string xmlName, string value)
{
if (QueryFolderIt(string.Concat(@"", path), xmlName))
{
InsertXML(string.Concat(@"",path), xmlName, value);
}
else
CreateXML(string.Concat(@"", path), xmlName, value);
}
public void CreateXML(string path, string xmlName, string value)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
//建立Xml的定义声明
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
xmlDoc.AppendChild(dec);
//创建根节点
XmlElement root = xmlDoc.CreateElement("BGHelper");
xmlDoc.AppendChild(root);
//创建根节点的子节点Book
XmlNode User = xmlDoc.CreateElement("User");
//创建Book的子节点
XmlElement Name = xmlDoc.CreateElement("Name");
Name.InnerText = value;
User.AppendChild(Name);
//将Book添加到根节点上
root.AppendChild(User);
//保存文档(如果已经存在该文件,则更新之;如果没有,则创建该文件)
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
public void InsertXML(string path, string xmlName, string value)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
XmlNode root = xmlDoc.SelectSingleNode("BGHelper");
XmlElement User = xmlDoc.CreateElement("User");
XmlElement Name = xmlDoc.CreateElement("Name");
Name.InnerText = value;
User.AppendChild(Name);
root.AppendChild(User);
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
public void UpdateXML(string path, string xmlName, string operValue, string newValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
//获取Books节点的所有子节点
XmlNodeList list = xmlDoc.SelectSingleNode("BGHelper").ChildNodes;//User
bool breakAll = false;
//遍历所有子节点
foreach (XmlNode node in list)
{
if (breakAll) break;
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)node;
foreach (XmlNode nd in xe.ChildNodes)
{
XmlElement x = (XmlElement)nd;
if (x.Name == "Name")
{
if (x.InnerText == operValue)
{
x.InnerText = newValue;
breakAll = true;
break;
}
}
}
}
//保存
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//删除
public void DeleteXML(string path, string xmlName, string operValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
//获取Books节点的所有子节点
XmlNodeList list = xmlDoc.SelectSingleNode("BGHelper").ChildNodes;//User
bool breakAll = false;
//遍历所有子节点
foreach (XmlNode node in list)
{
if (breakAll)
break;
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)node;
foreach (XmlNode xn in node.ChildNodes)
{
XmlElement x = (XmlElement)xn;
if (x.Name == "Name")
{
if (x.InnerText == operValue)
{
xe.RemoveAll();
breakAll = true;
break;
}
}
}
}
//保存
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//所有
public string ReadString(string path, string xmlName)
{
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(string.Concat(@"", path, xmlName));
string str = string.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.LocalName.Equals("Name"))
{
str += reader.ReadString() + ",";
}
}
}
if (str != string.Empty && str.Length > 0)
str = str.Substring(0, str.Length - 1);
return (str);
}
catch
{
throw;
}
finally
{
if (reader != null)
{
reader.Close();
reader = null;
}
}
}
//所有
public List<string> ReadStrings(string path, string xmlName)
{
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(string.Concat(@"", path, xmlName));
List<string> str = new List<string>();// string.Empty;
string reads = "";
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.LocalName.Equals("Name"))
{
reads = reader.ReadString();
if (reads != "")
str.Add(reads);
}
}
}
return (str);
}
catch
{
throw;
}
finally
{
if (reader != null)
{
reader.Close();
reader = null;
}
}
}
//所有,自定义,适用于单节点多子节点
public List<Entity.ListEntity> ReadStrings(string path, string xmlName, string[] xmlnodes)
{
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(string.Concat(@"", path, xmlName));
List<Entity.ListEntity> str = new List<Entity.ListEntity>();// string.Empty;
Entity.ListEntity le = null;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
foreach(string nodes in xmlnodes)
if (reader.LocalName.Equals(nodes))
{
le = new Entity.ListEntity();
le.IDName = nodes;
le.LEValue = reader.ReadString();
str.Add(le);//str.Add(reader.ReadString());
}
}
}
return (str);
}
catch
{
//throw;
return null;
}
finally
{
if (reader != null)
{
reader.Close();
reader = null;
}
}
}
public void ICAutoXML(string path, string xmlName,string nodesName, Entity.ListEntity[] value)
{
if (QueryFolderIt(string.Concat(@"", path), xmlName))
{
InsertXML(string.Concat(@"", path),nodesName, xmlName, value);
}
else
CreateXML(string.Concat(@"", path),nodesName, xmlName, value);
}
//删除还是新建
public void DCAutoXML(string path, string xmlName, string nodesName, Entity.ListEntity[] value)
{
if (QueryFolderIt(string.Concat(@"", path), xmlName))
{
new Folder().DeleteFile(string.Concat(@"", path,"//", xmlName));
CreateXML(string.Concat(@"", path), xmlName,nodesName, value);
}
else
CreateXML(string.Concat(@"", path), xmlName,nodesName, value);
}
public void CreateXML(string path, string xmlName, string nodesName, Entity.ListEntity[] value)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
//建立Xml的定义声明
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
xmlDoc.AppendChild(dec);
//创建根节点
XmlElement root = xmlDoc.CreateElement("BGHelper");
xmlDoc.AppendChild(root);
//创建根节点的子节点Book
XmlNode User = xmlDoc.CreateElement(nodesName);
for (int i = 0; i < value.Length; i++)
{
if (value[i].IDName != null&&value[i].LEValue!=null)
{
//创建Book的子节点
XmlElement Name = xmlDoc.CreateElement(value[i].IDName);
Name.InnerText = value[i].LEValue.ToString();
User.AppendChild(Name);
}
}
//将Book添加到根节点上
root.AppendChild(User);
//保存文档(如果已经存在该文件,则更新之;如果没有,则创建该文件)
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
//throw;
}
}
public void InsertXML(string path, string xmlName,string nodesName, Entity.ListEntity[] value)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
XmlNode root = xmlDoc.SelectSingleNode("BGHelper");
XmlElement User = xmlDoc.CreateElement(nodesName);
for (int i = 0; i < value.Length; i++)
{
XmlElement Name = xmlDoc.CreateElement(value[i].IDName);
Name.InnerText = value[i].LEValue.ToString();
User.AppendChild(Name);
}
root.AppendChild(User);
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//适用于单节点多子节点
public void UpdateXML(string path, string xmlName,string nodesName, Entity.ListEntity[] operValue, Entity.ListEntity[] newValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
//获取Books节点的所有子节点
XmlNodeList list = xmlDoc.SelectSingleNode(string.Concat("BGHelper//",nodesName)).ChildNodes;//User
bool breakAll = false;
//遍历所有子节点
foreach (XmlNode node in list)
{
if (breakAll) break;
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)node;
foreach (XmlNode nd in xe.ChildNodes)
{
XmlElement x = (XmlElement)nd;
for (int i = 0; i < operValue.Length; i++)
{
if (x.Name == operValue[i].IDName)
{
x.InnerText = newValue[i].LEValue.ToString();
breakAll = true;
break;
}
}
}
}
//保存
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//删除 适用于单节点多子节点
public void DeleteXML(string path, string xmlName, string nodesName, string operValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(string.Concat(@"", path, xmlName));
//获取Books节点的所有子节点
XmlNodeList list = xmlDoc.SelectSingleNode(string.Concat("BGHelper//", nodesName)).ChildNodes;//User
bool breakAll = false;
//遍历所有子节点
foreach (XmlNode node in list)
{
if (breakAll)
break;
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)node;
foreach (XmlNode xn in node.ChildNodes)
{
XmlElement x = (XmlElement)xn;
if (x.Name == "Name")
{
xe.RemoveAll();
breakAll = true;
break;
}
}
}
//保存
xmlDoc.Save(string.Concat(@"", path, xmlName));
}
catch
{
throw;
}
}
//直写 keyName元素名 keyValue值
public void WriteXML(string path, string xmlName, List<string> keyName, List<string> keyValue)
{
try
{
XmlTextWriter writer = new XmlTextWriter(string.Concat(@"", path, xmlName), null);
writer.Formatting = Formatting.Indented;
writer.Indentation = 6;
writer.WriteStartDocument();
writer.WriteStartElement("BGHelper");
writer.WriteStartElement("User");
for (int i = 0; i < keyName.Count; i++)
{
writer.WriteElementString(keyName[i], keyValue[i]);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
catch
{
throw;
}
}
}