您的位置:首页 > 其它

xml查找

2016-02-05 15:47 323 查看
using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;

public class SXML//节点类
{
public bool hasFound = true;//是否找到
private XmlNode rootNode;//根节点
private XmlNode curNode;//当前节点
private string NodeName;//你需要查找的节点的名字

private int filtOper;//1"==" 2"!=" 3">" 4"<" 5">=" 6"<="
private string filtKey = "";//需要过滤的键
private string filtValue = "";//需要过滤的键的值
private int filtIntValue;//需要过滤的键的值的int类型
public SXML(XmlNode root, string nodename, string filter)//初始化时,找到条件满足的第一个节点
{
rootNode = root;
curNode = root;
NodeName = nodename;

filtOper = 0;
if (filter != null)//如果过滤条件不是空
{
int nstep = 0;//是否已经找到运算符
char[] c_arr = filter.ToCharArray();
for (int i = 0; i < c_arr.Length; i++)
{
char curkey = c_arr[i];
switch (curkey)
{
case '=':
{
nstep = 1;
i++;
filtOper = 1; //"=="
} break;
case '!':
{
nstep = 1;
i++;
filtOper = 2; //"!="
} break;
case '>':
{
nstep = 1;
if ('=' == c_arr[i + 1])
{
i++;
filtOper = 5; //">="
}
else
{
filtOper = 3; //">"单个运算符i不需要加1
}
} break;
case '<':
{
nstep = 1;
if ('=' == c_arr[i + 1])
{
i++;
filtOper = 6; //"<="
}
else
{
filtOper = 4; //"<"
}
} break;
default:
{
if (1 == nstep)//已找到运算符
{
filtValue += curkey;//需要过滤的键的值
}
else
{
filtKey += curkey;//需要过滤的键
}
} break;
}
}

if (filtOper >= 3 && filtOper <= 6)//如果是比大小的运算符
{
filtIntValue = int.Parse(filtValue);//转换成int类型
}

if (false == checkFilt())//此节点如果不满足条件
{
nextOne();//下一个节点
}
}

}

private bool checkFilt()//计算节点的属性是否满足条件
{
if (curNode != null)//如果当前节点不为空
{
bool bpass = false;
switch (filtOper)
{
case 1:
{
if (true == getString(filtKey).Equals(filtValue))
{
bpass = true;
}
} break;
case 2:
{
if (false == getString(filtKey).Equals(filtValue))
{
bpass = true;
}
} break;
case 3:
{
int value = getInt(filtKey);
if (value > filtIntValue)
{
bpass = true;
}
} break;
case 4:
{
int value = getInt(filtKey);
if (value < filtIntValue)
{
bpass = true;
}
} break;
case 5:
{
int value = getInt(filtKey);
if (value >= filtIntValue)
{
bpass = true;
}
} break;
case 6:
{
int value = getInt(filtKey);
if (value <= filtIntValue)
{
bpass = true;
}
} break;
default:
{
bpass = false;
} break;
}
hasFound = bpass;//满足条件
return bpass;
}
hasFound = false;//不满足
return false;
}

public bool nextOne()//移到下一个满足条件的节点
{
while (curNode != null)
{
curNode = curNode.NextSibling;//移到下一个同级的节点

if (curNode != null && curNode.Name == NodeName)//如果节点不为空并且节点名字正确
{
if (filtOper != 0)//有运算符
{
if (checkFilt())//确认此节点是否满足条件
{
return true;
}
}
else
{
return true;
}
}
}
return false;
}

public SXML GetNode(string command, string filter)
{
if ( null == curNode )
return null;

XmlNode xml_node = curNode;
string[] cmds = command.Split('.');//用点切割命令

if (cmds.Length < 1) return null;//命令没有子节点

for (int i = 0; i < cmds.Length; i++)//按着深度一级级往下查找,如果找不到其中一个就返回空
{
bool bfind_node = false;//是否已经找到节点
foreach (XmlNode child in xml_node.ChildNodes)//遍历子节点
{
if (child.NodeType == XmlNodeType.Comment)
continue;

if (child.Name == cmds[i])
{
bfind_node = true;//找到节点
xml_node = child;//如果碰到节点名字一样的,把这个节点作为父节点
break;
}
}

if (false == bfind_node)
{
return null;
}
}

string lastkey = cmds[cmds.Length - 1];//最后一个节点的名字.也就是我们需要查找的节点
return new SXML(xml_node, lastkey, filter);//返回sxml实例
}

////////////////////////////////////////////////////////////////////////////////////////////////////
public string getString(string key)
{
if (curNode != null)
{
foreach (XmlAttribute attr in curNode.Attributes)
{
if (attr.Name == key)
{
return attr.Value;
}
}
}

return "null";
}

public float getFloat(string key)
{
if (curNode != null)
{
foreach (XmlAttribute attr in curNode.Attributes)
{
if (attr.Name == key)
{
return float.Parse(attr.Value);
}
}
}

return -1f;
}

public int getInt(string key)
{
if (curNode != null)
{
foreach (XmlAttribute attr in curNode.Attributes)
{
if (attr.Name == key)
{
return int.Parse(attr.Value);
}
}
}
return -1;
}

public uint getUint(string key)
{
if (curNode != null)
{
foreach (XmlAttribute attr in curNode.Attributes)
{
if (attr.Name == key)
{
return uint.Parse(attr.Value);
}
}
}

return 0;
}
}

public class XMLMgr//管理类,加载xml,获取节点
{
private static XMLMgr _instance;

public static XMLMgr instance
{
get
{
if(_instance==null)
{
_instance=new XMLMgr();
}
return _instance;
}
}
private XMLMgr()
{

}

private Dictionary<string, XmlDocument> m_allConf = new Dictionary<string, XmlDocument>();//配置文件字典

public void AddXmlData(string key, ref string data)//添加配置文件
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(data);//读路径
m_allConf[key] = xmlDoc;//放进字典
}

public SXML GetSXML(string command, string filter=null)
{
string[] cmds = command.Split('.');
if (cmds.Length < 2) return null;

if (m_allConf.ContainsKey(cmds[0]))
{
XmlDocument xml_doc = m_allConf[cmds[0]];

XmlNode xmlNode = xml_doc.DocumentElement;//获取根节点
for (int i = 1; i < cmds.Length; i++)//一级一级遍历
{
bool bfind_node = false;
foreach (XmlNode child in xmlNode.ChildNodes)
{
if (child.NodeType == XmlNodeType.Comment)
continue;

if (child.Name == cmds[i])
{
bfind_node = true;
xmlNode = child;
break;
}
}
if (false == bfind_node)
{
return null;
}
}
string lastkey = cmds[cmds.Length - 1];//最后一个节点的名字
return new SXML(xmlNode, lastkey, filter);//初始化sxml
}
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: