很多地方读取文件可能会出现并发现象 处理:
使用FileMode.Open, FileAccess.Read, FileShare.ReadWrite 避开并发
public static List<ConfigXml> GetXmlByTypeName(XmlConfigType type) { string path = string.Empty; try { path = HttpRuntime.BinDirectory + @"/xml/config.xml"; } catch (Exception e) { path = AppDomain.CurrentDomain.BaseDirectory + @"/xml/config.xml"; } FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); XmlDocument doc = new XmlDocument(); doc.Load(file); XmlElement root = doc.DocumentElement; XmlNodeList dataNode = doc.SelectSingleNode(String.Format("root/type[@name='{0}']", type.ToString())).ChildNodes; List<ConfigXml> list = new List<ConfigXml>(); foreach (XmlElement item in dataNode) { ConfigXml configXml = new ConfigXml(); configXml.Name = item.GetAttribute("name"); configXml.OnOff = item.InnerXml.Trim(); configXml.DateType = item.GetAttribute("dateType"); configXml.UrlValue =item.GetAttribute("Path"); configXml.IsBeingUsed = bool.Parse(item.GetAttribute("name")); list.Add(configXml); } file.Close(); return list; }
xml文件的一些操作
/// <summary> /// 查询监视文件信息 /// </summary> /// <param name="type"></param> /// <returns></returns> public static List<ConfigXml> GetXmlByTypeName() { string path = ConfigurationManager.ConnectionStrings["pathXml"].ConnectionString; FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); XmlDocument doc = new XmlDocument(); file.Seek(0, SeekOrigin.Begin); doc.Load(file); //Monitored节点下所有属性 XmlNodeList xnl = doc.GetElementsByTagName("Monitored"); List<ConfigXml> list = new List<ConfigXml>(); for (int i = 0; i < xnl.Count; i++) { //循环Monitored节点中每一子节点 得到具体信息 for (int j = 0; j < xnl[i].ChildNodes.Count; j++) { ConfigXml config = new ConfigXml(); XmlNode nodeCity = xnl[i].ChildNodes[j]; config.Name = nodeCity.Attributes["name"].Value; config.OnOff = nodeCity.ChildNodes[1].InnerXml.Trim(); config.DateType = nodeCity.ChildNodes[2].InnerXml.Trim(); config.UrlValue = nodeCity.ChildNodes[3].InnerXml.Trim(); if (config.Name == "newDisk") config.update = nodeCity.ChildNodes[4].InnerXml.Trim(); list.Add(config); } } file.Close(); file.Dispose(); return list; } /// <summary> /// 读取邮件信息 /// </summary> /// <returns></returns> public static Mail GetMail() { string path = ConfigurationManager.ConnectionStrings["pathXml"].ConnectionString; FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); XmlDocument doc = new XmlDocument(); doc.Load(file); //E-mail节点下所有属性 XmlNodeList xnl = doc.GetElementsByTagName("E-mail"); XmlNodeList mailXml = xnl[0].ChildNodes[0].ChildNodes; Mail mail = new Mail(); mail.Name = mailXml[0].InnerXml.Trim(); mail.Pwd = mailXml[1].InnerXml.Trim(); mail.Host = mailXml[2].InnerXml.Trim(); mail.mailAddress = mailXml[3].InnerXml.Trim(); file.Close(); file.Dispose(); return mail; } /// <summary> /// 关闭开关 /// </summary> /// <param name="Name">枚举</param> public static void UpdateXml(NameXMLType Name) { string xmlPath = ConfigurationManager.ConnectionStrings["pathXml"].ConnectionString; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); xmlDoc.SelectSingleNode("root/Monitored/type[@name='" + Name.ToString() + "']/onoff").InnerText = "no"; xmlDoc.Save(xmlPath); } /// <summary> /// 新房案例 是更新或插入 /// </summary> /// <returns></returns> public static string GetUpdatenewDisk() { string xmlPath = ConfigurationManager.ConnectionStrings["pathXml"].ConnectionString; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); return xmlDoc.SelectSingleNode("root/Monitored/type[@name='newDisk']/updata").InnerText; }