public static string GetConfigValue(string path, string nodeKey, string xelemKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(path);
XmlNode xNode = xDoc.SelectSingleNode("//" + nodeKey);
XmlElement xElem = (XmlElement)xNode.SelectSingleNode(xelemKey);
if (xElem != null)
{
return xElem.InnerText;
}
else
return "";
}
catch (Exception ex)
{
//LogHelper.Error(ex.Message);
return "";
}
}
<?xml version="1.0" encoding="utf-8" ?>
<SoftVersion>
<Version>1.1.1</Version>>
</SoftVersion>
说明:path为xml文本路径;nodeKey为父节点关键字,对应xml文档中的“SoftVersion”;xelemKey为所查找节点关键字,对应为xml文档中的“Version”。简单记录一下。
这段代码展示了如何使用C#从XML文件中加载数据并根据指定的父节点和子节点关键字查找元素。函数`GetConfigValue`通过加载XML文件,然后通过XPath选择指定的父节点和子节点,获取到`Version`节点的文本内容。如果找到节点,则返回其文本,否则返回空字符串。
142

被折叠的 条评论
为什么被折叠?



