首页 > 代码库 > xml读取 避开并发

xml读取 避开并发

 

很多地方读取文件可能会出现并发现象 处理:

使用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;                   }