/// <summary>
/// Author: jiangxiaoqiang
/// </summary>
public
class
XmlReader
{
//========================================================= //
#region 获取XmlDocument对象
/// <summary>
/// </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>
/// </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
{
}
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)
{
var childXmlNodes = GetChildNodesByName(parentXmlNode,childNodeName);
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)
{
if
(parentXmlNode ==
null
||
string
.IsNullOrEmpty(nodeName))
{
return
null
;
}
return
GetChildNodesByXPathExpr(parentXmlNode,
string
.Format(
".//{0}"
,nodeName));
}
/// <summary>
/// 获取父节点下满足xpathExpr表达式的XML子节点列表
/// </summary>
/// <param name="parentXmlNode">父节点</param>
/// <param name="xpathExpr"></param>
/// <returns></returns>
public
static
XmlNodeList GetChildNodesByXPathExpr(XmlNode parentXmlNode,
string
xpathExpr)
{
if
(parentXmlNode ==
null
||
string
.IsNullOrEmpty(xpathExpr))
{
return
null
;
}
return
parentXmlNode.SelectNodes(xpathExpr);
}
/// <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>
/// </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
attrName)
{
if
(parentXmlNode ==
null
||
string
.IsNullOrEmpty(childNodeName) ||
string
.IsNullOrEmpty(attrName))
{
return
null
;
}
var xpathExpr =
string
.Format(
"//{0}[@{1}]"
,attrName);
var nodes = GetChildNodesByXPathExpr(parentXmlNode,xpathExpr);
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>
/// 读取父节点下指定节点名的第一个子节点的文本
/// </summary>
/// <param name="parentXmlNode"></param>
/// <param name="childNodeName"></param>
/// <returns></returns>
public
static
string
ReadFirstChildNodeTextByName(XmlNode parentXmlNode,
string
childNodeName)
{
var childNodeTexts = ReadChildNodeTextsByName(parentXmlNode,childNodeName);