xml读取 避开并发(xml的一些操作)
2024-08-28 17:53:05
很多地方读取文件可能会出现并发现象 处理:
使用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(, SeekOrigin.Begin);
doc.Load(file); //Monitored节点下所有属性
XmlNodeList xnl = doc.GetElementsByTagName("Monitored");
List<ConfigXml> list = new List<ConfigXml>();
for (int i = ; i < xnl.Count; i++)
{
//循环Monitored节点中每一子节点 得到具体信息
for (int j = ; 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[].InnerXml.Trim();
config.DateType = nodeCity.ChildNodes[].InnerXml.Trim();
config.UrlValue = nodeCity.ChildNodes[].InnerXml.Trim();
if (config.Name == "newDisk")
config.update = nodeCity.ChildNodes[].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[].ChildNodes[].ChildNodes;
Mail mail = new Mail();
mail.Name = mailXml[].InnerXml.Trim();
mail.Pwd = mailXml[].InnerXml.Trim();
mail.Host = mailXml[].InnerXml.Trim();
mail.mailAddress = mailXml[].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; }
最新文章
- 分布式平台Spark环境的搭建
- Windows Azure Cloud Service (10) Role的生命周期
- css知多少(1)——我来问你来答
- 一个基于DDD的开源项目,各种技术!
- Ubuntu 上创建常用磁盘阵列
- linux 下各个工具使用(screen、tmux,pyenv、virtualenv,pip国内源,tree)
- hdu 3094 A tree game 博弈论
- C# 使用WIN32API设置外部程序窗口无边框
- Asp.net Mvc 第一回 安装,并使ASP.NET MVC页面运行起来
- Bull And Cows
- SqlCommand.Parameters.add()方法
- HDU - 5009 Paint Pearls(dp+优化双向链表)
- LDAP协议
- 小偷网站工具--Teleport Ultra
- LuoGu P2835 刻录光盘
- List查询重复值的个数,并根据重复的数目从多到少排列
- JavaScript之打印页面局部区域
- node.js中的框架
- TessorFlow学习 之 神经网络的构建
- php 性能优化
热门文章
- python_17_数据运算
- java对集合的操作,jxl操作excel
- angular实现全屏显示效果
- 使用IP地址方法登录MySQL数据库Can&#39;t connect to MySQL server的原因。mysql -h 192.168.1.104 -P3306 -uroot -p 失败
- Sass和gulp的简单了解
- SQL初次接触
- 生产环境LAMP搭建 - 基于 fastcgi
- 六、Linux 文件基本属性
- Zimber 8.8.12卸载后重新安装报错解决办法
- Mysql忘记密码找回步骤