using System;
using System.Data;
using System.IO;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
namespace Core.Helpers
{
public static class XmlHelper
{
public static XmlDocument CreateXmlDocument(string name, string type)
{
XmlDocument xmlDocument;
try
{
xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<" + name + "/>");
xmlDocument.DocumentElement?.SetAttribute(nameof (type), type);
}
catch (Exception ex)
{
throw ex;
}
return xmlDocument;
}
public static void Delete(string path, string node, string attribute)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNode oldChild = xmlDocument.SelectSingleNode(node);
XmlElement xmlElement = (XmlElement) oldChild;
if (attribute.Equals(""))
{
if (oldChild != null && oldChild.ParentNode != null)
oldChild.ParentNode.RemoveChild(oldChild);
}
else
xmlElement?.RemoveAttribute(attribute);
xmlDocument.Save(path);
}
catch (Exception ex)
{
throw ex;
}
}
public static DataSet GetDataSet(string source, XmlHelper.XmlType xmlType)
{
try
{
DataSet dataSet = new DataSet();
if (xmlType == XmlHelper.XmlType.File)
{
int num1 = (int) dataSet.ReadXml(source);
}
else
{
XmlDocument node = new XmlDocument();
node.LoadXml(source);
XmlNodeReader reader = new XmlNodeReader((XmlNode) node);
int num2 = (int) dataSet.ReadXml((XmlReader) reader);
}
return dataSet;
}
catch (Exception ex)
{
throw ex;
}
}
public static string GetNodeInfoByNodeName(string path, string nodeName)
{
try
{
string nodeInfoByNodeName = "";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlElement documentElement = xmlDocument.DocumentElement;
if (documentElement != null)
{
XmlNode xmlNode = documentElement.SelectSingleNode("//" + nodeName);
if (xmlNode != null)
nodeInfoByNodeName = xmlNode.InnerText;
}
return nodeInfoByNodeName;
}
catch (Exception ex)
{
throw ex;
}
}
public static string GetNodeValue(string source, XmlHelper.XmlType xmlType, string nodeName)
{
XmlDocument xmlDocument = new XmlDocument();
if (xmlType == XmlHelper.XmlType.File)
xmlDocument.Load(source);
else
xmlDocument.LoadXml(source);
XmlElement documentElement = xmlDocument.DocumentElement;
if (documentElement != null)
{
XmlNode xmlNode = documentElement.SelectSingleNode("//" + nodeName);
if (xmlNode != null)
return xmlNode.InnerText;
}
return "";
}
public static string GetNodeAttributeValue(
string source,
XmlHelper.XmlType xmlType,
string nodeName,
string attributeString)
{
XmlDocument xmlDocument = new XmlDocument();
if (xmlType == XmlHelper.XmlType.File)
xmlDocument.Load(source);
else
xmlDocument.LoadXml(source);
XmlElement documentElement = xmlDocument.DocumentElement;
if (documentElement != null)
{
XmlElement xmlElement = (XmlElement) documentElement.SelectSingleNode("//" + nodeName);
if (xmlElement != null)
return xmlElement.GetAttribute(attributeString);
}
return "";
}
public static string GetNodeValue(string source, string nodeName)
{
if (source == null || nodeName == null || source == "" || nodeName == "" || source.Length < nodeName.Length * 2)
return (string) null;
int startIndex = source.IndexOf("<" + nodeName + ">", StringComparison.Ordinal) + nodeName.Length + 2;
int num = source.IndexOf("" + nodeName + ">", StringComparison.Ordinal);
if (startIndex == -1 || num == -1)
return (string) null;
return startIndex < num ? source.Substring(startIndex, num - startIndex) : (string) null;
}
public static DataTable GetTable(string source, XmlHelper.XmlType xmlType, string tableName)
{
try
{
DataSet dataSet = new DataSet();
if (xmlType == XmlHelper.XmlType.File)
{
int num1 = (int) dataSet.ReadXml(source);
}
else
{
XmlDocument node = new XmlDocument();
node.LoadXml(source);
XmlNodeReader reader = new XmlNodeReader((XmlNode) node);
int num2 = (int) dataSet.ReadXml((XmlReader) reader);
}
return dataSet.Tables[tableName];
}
catch (Exception ex)
{
throw ex;
}
}
public static object GetTableCell(
string source,
XmlHelper.XmlType xmlType,
string tableName,
int rowIndex,
string colName)
{
try
{
DataSet dataSet = new DataSet();
if (xmlType == XmlHelper.XmlType.File)
{
int num1 = (int) dataSet.ReadXml(source);
}
else
{
XmlDocument node = new XmlDocument();
node.LoadXml(source);
XmlNodeReader reader = new XmlNodeReader((XmlNode) node);
int num2 = (int) dataSet.ReadXml((XmlReader) reader);
}
return dataSet.Tables[tableName].Rows[rowIndex][colName];
}
catch (Exception ex)
{
throw ex;
}
}
public static object GetTableCell(
string source,
XmlHelper.XmlType xmlType,
string tableName,
int rowIndex,
int colIndex)
{
try
{
DataSet dataSet = new DataSet();
if (xmlType == XmlHelper.XmlType.File)
{
int num1 = (int) dataSet.ReadXml(source);
}
else
{
XmlDocument node = new XmlDocument();
node.LoadXml(source);
XmlNodeReader reader = new XmlNodeReader((XmlNode) node);
int num2 = (int) dataSet.ReadXml((XmlReader) reader);
}
return dataSet.Tables[tableName].Rows[rowIndex][colIndex];
}
catch (Exception ex)
{
throw ex;
}
}
public static void GetXmlValueDataSet(string xmlString, ref DataSet dataSet)
{
try
{
XmlDocument node = new XmlDocument();
node.LoadXml(xmlString);
XmlNodeReader reader = new XmlNodeReader((XmlNode) node);
int num = (int) dataSet.ReadXml((XmlReader) reader);
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
public static void Insert(
string path,
string node,
string element,
string attribute,
string value)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNode xmlNode = xmlDocument.SelectSingleNode(node);
if (element.Equals(""))
{
if (!attribute.Equals(""))
((XmlElement) xmlNode)?.SetAttribute(attribute, value);
}
else
{
XmlElement element1 = xmlDocument.CreateElement(element);
if (attribute.Equals(""))
element1.InnerText = value;
else
element1.SetAttribute(attribute, value);
xmlNode?.AppendChild((XmlNode) element1);
}
xmlDocument.Save(path);
}
catch (Exception ex)
{
throw ex;
}
}
public static string Read(string path, string node, string attribute)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNode xmlNode = xmlDocument.SelectSingleNode(node);
return xmlNode == null ? "" : (attribute.Equals("") ? xmlNode.InnerText : (xmlNode.Attributes == null ? "" : xmlNode.Attributes[attribute].Value));
}
catch (Exception ex)
{
throw ex;
}
}
public static string ReadXml(string path, string nodeName, string name)
{
try
{
string str = "";
if (!File.Exists(path))
return str;
FileStream input = new FileStream(path, FileMode.Open);
XmlTextReader xmlTextReader = new XmlTextReader((Stream) input);
while (xmlTextReader.Read())
{
if (xmlTextReader.Name == nodeName)
{
str = xmlTextReader.GetAttribute(name);
break;
}
}
xmlTextReader.Close();
input.Close();
return str;
}
catch (Exception ex)
{
throw ex;
}
}
public static T ReadXML
{
try
{
return (T) new XmlSerializer(typeof (T)).Deserialize((TextReader) new StreamReader(path));
}
catch (Exception ex)
{
throw ex;
}
}
public static void SaveTableToFile(DataTable dataTable, string filePath)
{
try
{
new DataSet("Config")
{
Tables = {
dataTable.Copy()
}
}.WriteXml(filePath);
}
catch (Exception ex)
{
throw ex;
}
}
public static void SaveTableToFile(DataTable dataTable, string rootName, string filePath)
{
try
{
new DataSet(rootName)
{
Tables = {
dataTable.Copy()
}
}.WriteXml(filePath);
}
catch (Exception ex)
{
throw ex;
}
}
public static string SerializeToXmlStr
{
try
{
return XmlHelper.XmlSerialize
}
catch (Exception ex)
{
throw ex;
}
}
public static void Update(string path, string node, string attribute, string value)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlElement xmlElement = (XmlElement) xmlDocument.SelectSingleNode(node);
if (attribute.Equals(""))
{
if (xmlElement != null)
xmlElement.InnerText = value;
}
else
xmlElement?.SetAttribute(attribute, value);
xmlDocument.Save(path);
}
catch (Exception ex)
{
throw ex;
}
}
public static bool UpdateNode(string filePath, string nodeName, string nodeValue)
{
bool flag = false;
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(filePath);
XmlElement documentElement = xmlDocument.DocumentElement;
if (documentElement != null)
{
XmlNode xmlNode = documentElement.SelectSingleNode("//" + nodeName);
if (xmlNode != null)
{
xmlNode.InnerText = nodeValue;
flag = true;
}
else
flag = false;
}
return flag;
}
public static bool UpdateTableCell(
string filePath,
string tableName,
int rowIndex,
string colName,
string content)
{
DataSet dataSet = new DataSet();
int num = (int) dataSet.ReadXml(filePath);
DataTable table = dataSet.Tables[tableName];
bool flag;
if (table.Rows[rowIndex][colName] != null)
{
table.Rows[rowIndex][colName] = (object) content;
dataSet.WriteXml(filePath);
flag = true;
}
else
flag = false;
return flag;
}
public static bool UpdateTableCell(
string filePath,
string tableName,
int rowIndex,
int colIndex,
string content)
{
DataSet dataSet = new DataSet();
int num = (int) dataSet.ReadXml(filePath);
DataTable table = dataSet.Tables[tableName];
bool flag;
if (table.Rows[rowIndex][colIndex] != null)
{
table.Rows[rowIndex][colIndex] = (object) content;
dataSet.WriteXml(filePath);
flag = true;
}
else
flag = false;
return flag;
}
public static string WriteXML
{
if (string.IsNullOrEmpty(ends))
ends = "send";
int num = 0;
XmlSerializer xmlSerializer = new XmlSerializer(item.GetType());
string path1 = path + "\\" + jjdbh + ends + ".xml";
while (true)
{
try
{
File.Create(path1).Close();
TextWriter textWriter = (TextWriter) new StreamWriter(path1, false, Encoding.UTF8);
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
xmlSerializer.Serialize(textWriter, (object) item, namespaces);
textWriter.Flush();
textWriter.Close();
break;
}
catch (Exception ex)
{
if (num >= 5)
throw ex;
++num;
}
}
return XmlHelper.SerializeToXmlStr
}
public static T XmlDeserialize
public static T XmlFileDeserialize
{
try
{
byte[] buffer = XmlHelper.ShareReadFile(path);
if (buffer.Length < 1)
{
for (int index = 0; index < 5; ++index)
{
buffer = XmlHelper.ShareReadFile(path);
if (buffer.Length == 0)
Thread.Sleep(50);
else
break;
}
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load((Stream) new MemoryStream(buffer));
return xmlDocument.DocumentElement != null ? (T) new XmlSerializer(typeof (T)).Deserialize((XmlReader) new XmlNodeReader((XmlNode) xmlDocument.DocumentElement)) : default (T);
}
catch (Exception ex)
{
return default (T);
}
}
public static string XmlSerialize
{
XmlWriterSettings settings = new XmlWriterSettings()
{
OmitXmlDeclaration = omitXmlDeclaration,
Encoding = (Encoding) new UTF8Encoding(false)
};
MemoryStream output = new MemoryStream();
XmlWriter xmlWriter = XmlWriter.Create((Stream) output, settings);
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
new XmlSerializer(typeof (T)).Serialize(xmlWriter, (object) obj, namespaces);
return Encoding.UTF8.GetString(output.ToArray());
}
public static void XmlSerialize
string path,
T obj,
bool omitXmlDeclaration,
bool removeDefaultNamespace)
{
XmlWriterSettings settings = new XmlWriterSettings()
{
OmitXmlDeclaration = omitXmlDeclaration
};
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IndentChars = "\t";
using (XmlWriter xmlWriter = XmlWriter.Create(path, settings))
{
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
if (removeDefaultNamespace)
namespaces.Add(string.Empty, string.Empty);
new XmlSerializer(typeof (T)).Serialize(xmlWriter, (object) obj, namespaces);
}
}
private static byte[] ShareReadFile(string filePath)
{
byte[] buffer;
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
buffer = new byte[fileStream.Length];
int length = (int) fileStream.Length;
int offset = 0;
int num;
for (; length > 0; length -= num)
{
num = fileStream.Read(buffer, offset, length);
if (num != 0)
offset += num;
else
break;
}
}
return buffer;
}
public enum XmlType
{
File,
String,
}
}
}