You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

838 lines
29 KiB
C#

9 months ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Xml;
using System.IO;
/// <summary>
/// XMLParser 简易XML解析方法
///string str = File.ReadAllText(@"config.xml", Encoding.UTF8); //读取XML文件
/////MessageBox.Show(str);
///XMLParser xmlParser = new XMLParser();
///XMLNode xn = xmlParser.Parse(str);
///server = xn.GetValue("items>0>server>0>_text");
/// database = xn.GetValue("items>0>database>0>_text");
/// XMLNode temp = xn.GetNode("items>0>res>0");
///string basePath = temp.GetValue("@basePath");//或直接 basePath=xn.GetValue("items>0>res>0>@basePath");
///当然xml文件内容为
///<? xml version="1.0" encoding="utf-8" ?>
///<items>
/// <server>192.168.52.148</server>
/// <database>world</database>
/// <port>3306</port>
/// <uid>wtx</uid>
/// <password>123456</password>
/// <res basePath = "d:\Resources" language="zh_CN" />
/// <res basePath1 = "d:\Resources" language="zh_CN" />
/// <res basePath2 = "d:\Resources" language="zh_CN" />
/// </items>
/// </summary>
namespace Job_JieFeng_FTP
{
public class NoRepeatStrList : List<string>
{
public void _add(string str)
{
if (IndexOf(str) < 0) Add(str);
}
public string getstr()
{
var result = "";
foreach (string line in this)
{
if (result != "") result += "\r\n";
result += line;
}
return result;
}
}
/*
public class XMLParser
{
private char LT = '<';
private char GT = '>';
private char SPACE = ' ';
private char QUOTE = '"';
private char QUOTE2 = '\'';
private char SLASH = '/';
private char QMARK = '?';
private char EQUALS = '=';
private char EXCLAMATION = '!';
private char DASH = '-';
//private char SQL = '[';
private char SQR = ']';
public XMLNode Parse(string content)
{
XMLNode rootNode = new XMLNode();
rootNode["_text"] = "";
string nodeContents = "";
bool inElement = false;
bool collectNodeName = false;
bool collectAttributeName = false;
bool collectAttributeValue = false;
bool quoted = false;
string attName = "";
string attValue = "";
string nodeName = "";
string textValue = "";
bool inMetaTag = false;
bool inComment = false;
bool inCDATA = false;
XMLNodeList parents = new XMLNodeList();
XMLNode currentNode = rootNode;
for (int i = 0; i < content.Length; i++)
{
char c = content[i];
char cn = '~'; // unused char
char cnn = '~'; // unused char
char cp = '~'; // unused char
if ((i + 1) < content.Length) cn = content[i + 1];
if ((i + 2) < content.Length) cnn = content[i + 2];
if (i > 0) cp = content[i - 1];
if (inMetaTag)
{
if (c == QMARK && cn == GT)
{
inMetaTag = false;
i++;
}
continue;
}
else
{
if (!quoted && c == LT && cn == QMARK)
{
inMetaTag = true;
continue;
}
}
if (inComment)
{
if (cp == DASH && c == DASH && cn == GT)
{
inComment = false;
i++;
}
continue;
}
else
{
if (!quoted && c == LT && cn == EXCLAMATION)
{
if (content.Length > i + 9 && content.Substring(i, 9) == "<![CDATA[")
{
inCDATA = true;
i += 8;
}
else
{
inComment = true;
}
continue;
}
}
if (inCDATA)
{
if (c == SQR && cn == SQR && cnn == GT)
{
inCDATA = false;
i += 2;
continue;
}
textValue += c;
continue;
}
if (inElement)
{
if (collectNodeName)
{
if (c == SPACE)
{
collectNodeName = false;
}
else if (c == GT)
{
collectNodeName = false;
inElement = false;
}
if (!collectNodeName && nodeName.Length > 0)
{
if (nodeName[0] == SLASH)
{
// close tag
if (textValue.Length > 0)
{
currentNode["_text"] += textValue;
}
textValue = "";
nodeName = "";
currentNode = parents.Pop();
}
else
{
if (textValue.Length > 0)
{
currentNode["_text"] += textValue;
}
textValue = "";
XMLNode newNode = new XMLNode();
newNode["_text"] = "";
newNode["_name"] = nodeName;
if (currentNode[nodeName] == null)
{
currentNode[nodeName] = new XMLNodeList();
}
XMLNodeList a = (XMLNodeList)currentNode[nodeName];
a.Push(newNode);
parents.Push(currentNode);
currentNode = newNode;
nodeName = "";
}
}
else
{
nodeName += c;
}
}
else
{
if (!quoted && c == SLASH && cn == GT)
{
inElement = false;
collectAttributeName = false;
collectAttributeValue = false;
if (attName.Length > 0)
{
if (attValue.Length > 0)
{
currentNode["@" + attName] = attValue;
}
else
{
currentNode["@" + attName] = true;
}
}
i++;
currentNode = parents.Pop();
attName = "";
attValue = "";
}
else if (!quoted && c == GT)
{
inElement = false;
collectAttributeName = false;
collectAttributeValue = false;
if (attName.Length > 0)
{
currentNode["@" + attName] = attValue;
}
attName = "";
attValue = "";
}
else
{
if (collectAttributeName)
{
if (c == SPACE || c == EQUALS)
{
collectAttributeName = false;
collectAttributeValue = true;
}
else
{
attName += c;
}
}
else if (collectAttributeValue)
{
if (c == QUOTE || c == QUOTE2)
{
if (quoted)
{
collectAttributeValue = false;
currentNode["@" + attName] = attValue;
attValue = "";
attName = "";
quoted = false;
}
else
{
quoted = true;
}
}
else
{
if (quoted)
{
attValue += c;
}
else
{
if (c == SPACE)
{
collectAttributeValue = false;
currentNode["@" + attName] = attValue;
attValue = "";
attName = "";
}
}
}
}
else if (c == SPACE)
{
}
else
{
collectAttributeName = true;
attName = "" + c;
attValue = "";
quoted = false;
}
}
}
}
else
{
if (c == LT)
{
inElement = true;
collectNodeName = true;
}
else
{
textValue += c;
}
}
}
return rootNode;
}
/// <summary>
/// 传入一个node列表 和一个 路径 一个值
/// 返回节点列表里面,【该路径=该值】的节点
/// </summary>
/// <param name="nodelist"></param>
/// <param name="path"></param>
/// <param name="value"></param>
/// <param name="xn"></param>
/// <returns></returns>
public static bool getNode(XMLNodeList nodelist, string path, string value,out XMLNode xn) {
xn = new XMLNode();
foreach (XMLNode nd in nodelist) {
if (nd.GetValue(path) == value) {
xn = nd;
return true;
}
}
return false;
}
}
public class XMLNode : Hashtable
{
public XMLNodeList GetNodeList(string path)
{
return GetObject(path) as XMLNodeList;
}
public XMLNode GetNode(string path)
{
return GetObject(path) as XMLNode;
}
public string GetValue(string path)
{
return GetObject(path) as string;
}
private object GetObject(string path)
{
string[] bits = path.Split('>');
XMLNode currentNode = this;
XMLNodeList currentNodeList = null;
bool listMode = false;
object ob;
for (int i = 0; i < bits.Length; i++)
{
if (listMode)
{
currentNode = (XMLNode)currentNodeList[int.Parse(bits[i])];
ob = currentNode;
listMode = false;
}
else
{
ob = currentNode[bits[i]];
if (ob is ArrayList)
{
currentNodeList = (XMLNodeList)(ob as ArrayList);
listMode = true;
}
else
{
// reached a leaf node/attribute
if (i != (bits.Length - 1))
{
// unexpected leaf node
string actualPath = "";
for (int j = 0; j <= i; j++)
{
actualPath = actualPath + ">" + bits[j];
}
//Debug.Log("xml path search truncated. Wanted: " + path + " got: " + actualPath);
}
return ob;
}
}
}
if (listMode)
return currentNodeList;
else
return currentNode;
}
}
public class XMLNodeList : ArrayList
{
public XMLNode Pop()
{
XMLNode item = null;
item = (XMLNode)this[this.Count - 1];
this.Remove(item);
return item;
}
public int Push(XMLNode item)
{
Add(item);
return this.Count;
}
}
*/
/// <summary>
/// Author: jiangxiaoqiang
/// </summary>
public class XmlReader
{
//========================================================= //
#region 获取XmlDocument对象
/// <summary>
/// 根据XML文件内容获取XmlDocument对象
/// </summary>
/// <param name="xmlFileContent"></param>
/// <returns></returns>
public static XmlDocument GetXmlDocByXmlContent(string xmlFileContent)
{
if (string.IsNullOrEmpty(xmlFileContent))
{
return null;
}
var xDoc = new XmlDocument();
try
{
xDoc.LoadXml(xmlFileContent);
}
catch
{
xDoc = null;
}
return xDoc;
}
/// <summary>
/// 根据XML文件路径获取XmlDocument对象
/// </summary>
/// <param name="xmlFilePath"></param>
/// <returns></returns>
public static XmlDocument GetXmlDocByFilePath(string xmlFilePath)
{
if (string.IsNullOrEmpty(xmlFilePath) || !File.Exists(xmlFilePath))
{
return null;
}
var xDoc = new XmlDocument();
try
{
xDoc.Load(xmlFilePath);
}
catch
{
throw new Exception(string.Format("请确认该XML文件格式正确路径为{0}", xmlFilePath));
}
return xDoc;
}
#endregion
//========================================================= //
//========================================================= //
#region 获取XML节点或节点列表
/// <summary>
/// 获取父节点下指定节点名称的第一个子节点
/// </summary>
/// <param name="parentXmlNode"></param>
/// <param name="childNodeName"></param>
/// <returns></returns>
public static XmlNode GetFirstChildNodeByName(XmlNode parentXmlNode, string childNodeName, XmlNamespaceManager npm = null)
{
var childXmlNodes = GetChildNodesByName(parentXmlNode, childNodeName,npm);
if (childXmlNodes != null && childXmlNodes.Count > 0)
{
return childXmlNodes[0];
}
return null;
}
/// <summary>
/// 获取父节点下指定节点名称的子节点列表
/// </summary>
/// <param name="parentXmlNode">父节点</param>
/// <param name="nodeName">节点名称</param>
/// <returns></returns>
public static XmlNodeList GetChildNodesByName(XmlNode parentXmlNode, string nodeName, XmlNamespaceManager npm = null)
{
if (parentXmlNode == null || string.IsNullOrEmpty(nodeName))
{
return null;
}
return GetChildNodesByXPathExpr(parentXmlNode, string.Format(".//{0}", nodeName), npm);
//return GetChildNodesByXPathExpr(parentXmlNode, string.Format("//{0}", nodeName), npm);
}
/// <summary>
/// 获取父节点下满足xpathExpr表达式的XML子节点列表
/// </summary>
/// <param name="parentXmlNode">父节点</param>
/// <param name="xpathExpr"></param>
/// <returns></returns>
public static XmlNodeList GetChildNodesByXPathExpr(XmlNode parentXmlNode, string xpathExpr, XmlNamespaceManager npm=null)
{
if (parentXmlNode == null || string.IsNullOrEmpty(xpathExpr))
{
return null;
}
return parentXmlNode.SelectNodes(xpathExpr,npm);
}
/// <summary>
/// 获取父节点下的第一个子节点
/// </summary>
/// <param name="parentXmlNode"></param>
/// <returns></returns>
public static XmlNode GetFirstChildNode(XmlNode parentXmlNode)
{
var childXmlNodes = GetChildNodes(parentXmlNode);
if (childXmlNodes != null && childXmlNodes.Count > 0)
{
return childXmlNodes[0];
}
return null;
}
/// <summary>
/// 获取父节点的子节点列表
/// </summary>
/// <param name="parentXmlNode">父节点</param>
/// <returns></returns>
public static XmlNodeList GetChildNodes(XmlNode parentXmlNode)
{
return parentXmlNode == null ? null : parentXmlNode.ChildNodes;
}
#endregion
//========================================================= //
//========================================================= //
#region 读取节点属性值
/// <summary>
/// 读取某个XML节点的属性值根据属性名
/// </summary>
/// <param name="xmlNode"></param>
/// <param name="attrName"></param>
/// <returns></returns>
public static string ReadAttrValue(XmlNode xmlNode, string attrName)
{
var xmlElement = xmlNode as XmlElement;
return xmlElement == null ? null : xmlElement.GetAttribute(attrName);
}
/// <summary>
/// 读取父节点下指定节点名和属性名的第一个子节点的属性值
/// </summary>
/// <param name="parentXmlNode">XML父节点</param>
/// <param name="childNodeName">节点名称</param>
/// <param name="attrName">属性名</param>
/// <returns></returns>
public static string ReadFirstAttrValue(XmlNode parentXmlNode, string childNodeName, string attrName)
{
var attrVals = ReadAttrValues(parentXmlNode, childNodeName, attrName);
return (attrVals == null || attrVals.Length == 0) ? null : attrVals[0];
}
/// <summary>
/// 读取父节点下指定节点名和属性名的所有子节点的该属性值的数组
/// </summary>
/// <param name="parentXmlNode">XML文档</param>
/// <param name="childNodeName">节点名称</param>
/// <param name="attrName">属性名</param>
/// <returns></returns>
public static string[] ReadAttrValues(XmlNode parentXmlNode, string childNodeName, string attrName, XmlNamespaceManager npm = null)
{
if (parentXmlNode == null || string.IsNullOrEmpty(childNodeName) || string.IsNullOrEmpty(attrName))
{
return null;
}
var xpathExpr = string.Format("//{0}[@{1}]", childNodeName, attrName);
var nodes = GetChildNodesByXPathExpr(parentXmlNode, xpathExpr, npm);
if (nodes != null && nodes.Count > 0)
{
var nodeCount = nodes.Count;
var attrVals = new string[nodeCount];
for (var i = 0; i < nodeCount; i++)
{
attrVals[i] = ((XmlElement)nodes[i]).GetAttribute(attrName);
}
return attrVals;
}
return null;
}
#endregion
#region 扩展方法
/// <summary>
/// 返回一个节点这个节点是【作为参数的父节点】下面的所有【名称为NodeName】的子节点当中第一个具有【指定属性名AttrName=指定属性值AttrValue】的
/// </summary>
/// <param name="parentXmlNode"></param>
/// <param name="AttrName"></param>
/// <param name="AttrValue"></param>
/// <returns></returns>
public static XmlNode getNodeByAttr(XmlNode parentXmlNode,string NodeName, string AttrName, string AttrValue, XmlNamespaceManager npm = null)
{
XmlNodeList nodelist = GetChildNodesByName(parentXmlNode, NodeName, npm);
foreach (XmlNode _item in nodelist)
{
var _attrvalue = ReadAttrValue(_item, AttrName);
if (_attrvalue == AttrValue)
{
return _item;
}
}
return null;
}
//
public static string getmlstr(XmlNode node)
{
var stringarray = XmlReader.ReadChildNodeTexts(node);
var result = "";
foreach (string line in stringarray)
{
if (result != "") result += "\r\n";
result += line;
}
return result;
}
public static string getmlstr(XmlNodeList nodeList)
{
var strlist = new NoRepeatStrList();
foreach (XmlNode node in nodeList) {
var _r = getmlstr(node);
strlist._add(_r);
}
return strlisttostr(strlist);
}
public static string strlisttostr(List<string> strlist) {
var result = "";
foreach (string line in strlist)
{
if (result != "") result += "\r\n";
result += line;
}
return result;
}
#endregion
//========================================================= //
//========================================================= //
#region 读取父节点下的子节点的文本内容
/// <summary>
/// 读取父节点下指定节点名的第一个子节点的文本
/// </summary>
/// <param name="parentXmlNode"></param>
/// <param name="childNodeName"></param>
/// <returns></returns>
public static string ReadFirstChildNodeTextByName(XmlNode parentXmlNode, string childNodeName, XmlNamespaceManager npm = null)
{
var childNodeTexts = ReadChildNodeTextsByName(parentXmlNode, childNodeName,npm);
if (childNodeTexts != null && childNodeTexts.Length > 0)
{
return childNodeTexts[0];
}
return null;
}
/// <summary>
/// 读取第N个指定节点名的子节点的文本
/// </summary>
/// <param name="parentXmlNode"></param>
/// <param name="childNodeName"></param>
/// <returns></returns>
public static string ReadNChildNodeTextByName(XmlNode parentXmlNode, string childNodeName,int N)
{
var childNodeTexts = ReadChildNodeTextsByName(parentXmlNode, childNodeName);
if (childNodeTexts != null && childNodeTexts.Length > N-1)
{
return childNodeTexts[N];
}
return null;
}
/// <summary>
/// 读取父节点下指定节点名的所有子节点的文本数组
/// </summary>
/// <param name="parentXmlNode"></param>
/// <param name="childNodeName"></param>
/// <returns></returns>
public static string[] ReadChildNodeTextsByName(XmlNode parentXmlNode, string childNodeName, XmlNamespaceManager npm = null)
{
if (parentXmlNode == null || string.IsNullOrEmpty(childNodeName))
{
return null;
}
var xpathExpr = string.Format(".//{0}", childNodeName);
var childNodes = GetChildNodesByXPathExpr(parentXmlNode, xpathExpr, npm);
if (childNodes != null && childNodes.Count > 0)
{
var nodeCount = childNodes.Count;
var nodeTexts = new string[nodeCount];
for (var i = 0; i < nodeCount; i++)
{
nodeTexts[i] = childNodes[i].InnerText;
}
return nodeTexts;
}
return null;
}
/// <summary>
/// 读取父节点下的第一个子节点的文本
/// </summary>
/// <param name="parentXmlNode"></param>
/// <returns></returns>
public static string ReadFirstChildNodeText(XmlNode parentXmlNode)
{
var childNodeTexts = ReadChildNodeTexts(parentXmlNode);
if (childNodeTexts != null && childNodeTexts.Length > 0)
{
return childNodeTexts[0];
}
return null;
}
/// <summary>
/// 读取父节点下的所有子节点的文本数组
/// </summary>
/// <param name="parentXmlNode"></param>
/// <returns></returns>
public static string[] ReadChildNodeTexts(XmlNode parentXmlNode)
{
if (parentXmlNode == null)
{
return null;
}
var childNodes = GetChildNodes(parentXmlNode);
if (childNodes != null && childNodes.Count > 0)
{
var nodeCount = childNodes.Count;
var nodeTexts = new string[nodeCount];
for (var i = 0; i < nodeCount; i++)
{
nodeTexts[i] = childNodes[i].InnerText;
}
return nodeTexts;
}
return null;
}
public static string ReadChildNodeTextsStr(XmlNode parentXmlNode)
{
if (parentXmlNode == null)
{
return null;
}
var result = "";
var childNodes = GetChildNodes(parentXmlNode);
if (childNodes != null && childNodes.Count > 0)
{
var nodeCount = childNodes.Count;
var nodeTexts = new string[nodeCount];
for (var i = 0; i < nodeCount; i++)
{
nodeTexts[i] = childNodes[i].InnerText;
}
foreach (string s in nodeTexts) {
if (result != "") result += "\r\n";
result += s;
}
return result;
}
return null;
}
/// <summary>
/// 读取XML节点文本
/// </summary>
/// <param name="xmlNode"></param>
/// <returns></returns>
public static string ReadNodeText(XmlNode xmlNode)
{
if (xmlNode == null)
{
return null;
}
return xmlNode.InnerText;
}
#endregion
//========================================================= //
}
}