post

ASP.NET常用方法

平时工作中常用到的方法,如验证邮箱地址,读写文本文件,读取XML配置节


/// 获取xml节点集合
/// xml文件路径       
/// xpath    
public static XmlNodeList GetXmlNodeList(string xmlPath, string xpath)
{
var xmlDocument = new XmlDocument();
xmlDocument.Load(xmlPath);
var node = xmlDocument.SelectSingleNode(xpath);
if (node != null && node.HasChildNodes)
{
return node.ChildNodes;
}
return null;
}

///
/// 检查是否是email
///
/// email        
/// 是否是email
public static bool CheckIsMail(string s)
{
const string regex = "^[_\\.0-9a-zA-Z+-]+@([0-9a-zA-Z]+[0-9a-zA-Z-]*\\.)+[a-zA-Z]{2,4}$";
return Regex.IsMatch(s, regex);
}

/// 获取文本内容
///
///  
private static string GetTxtFromFile(string fileName)
{
var thePath = Path.Combine(Environment.CurrentDirectory, fileName);
var reader = new StreamReader(thePath, Encoding.GetEncoding("gb2312"));
var result = reader.ReadToEnd();
reader.Close();
reader.Dispose();
return result;
}

/// 将内容存入文本文件
///
/// 
///  
private static void SaveTxt(string content, string fileName)
{
var thePath = Path.Combine(Environment.CurrentDirectory, fileName);
var writer = new StreamWriter(thePath, false, Encoding.GetEncoding("gb2312"));
writer.Write(content);
writer.Close();
writer.Dispose();
}
· 602 次浏览