读取xml文件中节点

时间:2021-12-06 12:04:47
/// <summary>
///
/// </summary>
/// <param name="xmlpath">节点路径</param>
/// <param name="getLatest"></param>
/// <returns></returns>
public static string GetPropertyValue(string xmlpath, bool getLatest = false) {
XmlDocument xml
= new XmlDocument();
if (string.IsNullOrEmpty(_xmlFilePath)||!System.IO.File.Exists(_xmlFilePath))
{
return null;
}
try
{
xml.Load(_xmlFilePath);
XmlNodeList nodes
= xml.SelectNodes(xmlpath);
if (nodes==null)
{
throw new AggregateException(string.Format("未找到文件XMLFile1.xml或不存在节点{0}", xmlpath));
}
return nodes[0].InnerXml;
}
catch (Exception e)
{
throw new System.Exception(string.Format("请确认该XML文件格式正确,路径为:{0}", _xmlFilePath));
}
}