📄 xmlmanage.cs
字号:
namespace PowerEasy.Components
{
using PowerEasy.Enumerations;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using System.Xml.XPath;
public sealed class XmlManage
{
private string filePath;
private XmlDocument xmlDoc = new XmlDocument();
private XmlManage()
{
}
public static bool CheckXmlDataSource(string datasource)
{
if (!string.IsNullOrEmpty(datasource))
{
string str2;
XmlDocument document = new XmlDocument();
string pattern = @"^http:\/\/(.*?)";
if (Regex.IsMatch(datasource.ToLower(), pattern))
{
bool flag = false;
Uri requestUri = new Uri(datasource);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(requestUri);
try
{
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
if ("OK" == response.StatusCode.ToString())
{
flag = true;
}
}
catch (WebException)
{
return false;
}
if (flag)
{
try
{
document.Load(datasource);
return true;
}
catch (XmlException)
{
return false;
}
}
return false;
}
HttpContext current = HttpContext.Current;
if (current != null)
{
str2 = current.Server.MapPath("~/" + datasource);
}
else
{
str2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, datasource);
}
if (System.IO.File.Exists(str2))
{
try
{
document.Load(str2);
return true;
}
catch (XmlException)
{
return false;
}
}
}
return false;
}
public bool ExistsNode(string nodeName)
{
if (this.xmlDoc.SelectSingleNode(nodeName) == null)
{
return false;
}
return true;
}
public DataTable GetAllNodeAndTheFirstAttribute(string nodeName, string attribute)
{
DataTable table = new DataTable();
DataColumn column = new DataColumn("NodeName", typeof(string));
DataColumn column2 = new DataColumn("NodeValue", typeof(string));
DataColumn column3 = new DataColumn("Attribute", typeof(string));
table.Columns.Add(column);
table.Columns.Add(column2);
table.Columns.Add(column3);
if (string.IsNullOrEmpty(nodeName))
{
foreach (System.Xml.XmlNode node2 in this.xmlDoc.DocumentElement.ChildNodes)
{
DataRow row = table.NewRow();
row["NodeName"] = node2.Name.ToLower();
row["NodeValue"] = node2.InnerText;
row["Attribute"] = node2.Attributes[attribute].Value;
table.Rows.Add(row);
}
}
else
{
System.Xml.XmlNode node4 = this.xmlDoc.DocumentElement.SelectSingleNode(nodeName);
if ((node4 != null) && node4.HasChildNodes)
{
foreach (System.Xml.XmlNode node5 in node4)
{
DataRow row2 = table.NewRow();
row2["NodeName"] = node5.Name;
row2["NodeValue"] = node5.InnerText;
row2["Attribute"] = node5.Attributes[attribute].Value;
table.Rows.Add(row2);
}
}
}
table.AcceptChanges();
return table;
}
public DataTable GetAllNodeValue(string nodeName)
{
DataTable table = new DataTable();
DataColumn column = new DataColumn("NodeName", typeof(string));
DataColumn column2 = new DataColumn("NodeValue", typeof(string));
table.Columns.Add(column);
table.Columns.Add(column2);
if (string.IsNullOrEmpty(nodeName))
{
foreach (System.Xml.XmlNode node2 in this.xmlDoc.DocumentElement.ChildNodes)
{
DataRow row = table.NewRow();
row["NodeName"] = node2.Name.ToLower();
row["NodeValue"] = node2.InnerText;
table.Rows.Add(row);
}
}
else
{
System.Xml.XmlNode node4 = this.xmlDoc.DocumentElement.SelectSingleNode(nodeName);
if (node4.HasChildNodes)
{
foreach (System.Xml.XmlNode node5 in node4)
{
DataRow row2 = table.NewRow();
row2["NodeName"] = node5.Name;
row2["NodeValue"] = node5.InnerText;
table.Rows.Add(row2);
}
}
}
table.AcceptChanges();
return table;
}
public Dictionary<string, string> GetAttributesValue(string nodeName)
{
if (string.IsNullOrEmpty(nodeName))
{
return null;
}
System.Xml.XmlNode node = this.xmlDoc.SelectSingleNode(nodeName);
if (node.Attributes == null)
{
return null;
}
XmlAttributeCollection attributes = node.Attributes;
Dictionary<string, string> dictionary = new Dictionary<string, string>();
foreach (XmlAttribute attribute in attributes)
{
dictionary.Add(attribute.Name, attribute.Value);
}
return dictionary;
}
public string GetAttributesValue(string nodeName, string arrtibuteName)
{
if (!string.IsNullOrEmpty(nodeName) && !string.IsNullOrEmpty(arrtibuteName))
{
return this.xmlDoc.SelectSingleNode(nodeName).Attributes.GetNamedItem(arrtibuteName).Value.ToLower().Trim();
}
return null;
}
public static string GetAttributesValue(IXPathNavigable node, string name)
{
if (!string.IsNullOrEmpty(name))
{
XmlElement element = (XmlElement) node;
if (element.HasAttribute(name))
{
return element.Attributes.GetNamedItem(name).Value;
}
}
return string.Empty;
}
public string GetNodeValue(string path)
{
if (string.IsNullOrEmpty(path))
{
return string.Empty;
}
try
{
return this.xmlDoc.SelectSingleNode(path).InnerText;
}
catch (NullReferenceException)
{
return string.Empty;
}
}
public string GetSingleNodeValue(string nodeName)
{
if (string.IsNullOrEmpty(nodeName))
{
return null;
}
if (this.xmlDoc.SelectSingleNode(nodeName) != null)
{
return this.xmlDoc.SelectSingleNode(nodeName).InnerText;
}
return string.Empty;
}
public static IList<XmlScheme> GetXmlTree(IXPathNavigable inputNode, int loopdeep, int outdeep, string xpath, int outype, int stat)
{
IList<XmlScheme> list = new List<XmlScheme>();
if ((outdeep == 0) || (outdeep > 100))
{
outdeep = 100;
}
if (loopdeep < outdeep)
{
System.Xml.XmlNode node = (System.Xml.XmlNode) inputNode;
int num = loopdeep;
num++;
XmlScheme item = new XmlScheme();
item.Level = loopdeep;
item.Station = stat;
item.Path = xpath;
item.Name = node.Name;
item.Text = node.InnerText;
if (!node.HasChildNodes)
{
item.Type = "onlyone";
list.Add(item);
return list;
}
XmlNodeList childNodes = node.ChildNodes;
IList<System.Xml.XmlNode> list3 = new List<System.Xml.XmlNode>();
bool flag = false;
foreach (System.Xml.XmlNode node2 in childNodes)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -