xml文件主要结构如下
<config>
<item key="test" value="test"/>
<item key="test" value="test"/>
</config>
一 读取
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
二 转换到list
先建立一个实体类
public class QueueXml
{
public string Key
{
get;
set;
}
public string Value
{
get;
set;
}
}
然后
List<QueueXml> list = new List<QueueXml>();
xmlDoc.Load(fileName);
foreach (XmlNode node in xmlDoc.SelectNodes("/Config/Item"))
{
QueueXml queueXml = new QueueXml();
queueXml.Key=node.Attributes["key"].Value;
queueXml.Value=node.Attributes["value"].Value;
list.Add(queueXml);
}
这样就可以了。
三 寻找节点
string xPath=string.Format("Config/Item[@key='{0}']",“test”)
XmlNode node=xmlDoc.SelectSingleNode(xPath);
四 移除节点并保存
string xPath=string.Format("Config/Item[@key='{0}']",“test”)
XmlNode node=xmlDoc.SelectSingleNode(xPath);
xmlDoc.SelectSingleNode("/Config").RemoveChild(node);
this.xmlDoc.Save(fileName);
五 建立节点并保存
XmlAttribute attrKey= xmlDoc.CreateAttribute("key");
attrKey.Value = str;
XmlAttribute attrValue= xmlDoc.CreateAttribute("value");
attrValue.Value = str;
XmlNode node= xmlDoc.CreateElement("Item");
node.Attributes.Append(attrKey);
node.Attributes.Append(attrValue);
xmlDoc.SelectSingleNode("/Config").AppendChild(node);
this.xmlDoc.Save(fileName);
posted @ 2011-07-19 15:11 chenleinet 阅读(16) 评论(0)
编辑
private void ExecuteCMD(string cmd)
{
Process process = new Process();
try
{
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
// process.StartInfo.CreateNoWindow = true;
process.Start();
process.StandardInput.WriteLine(cmd);
process.StandardInput.WriteLine("exit");
process.WaitForExit();
string readProOutResult = process.StandardOutput.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show("操作失败");
}
finally
{
process.Close();
}
posted @ 2011-07-19 15:03 chenleinet 阅读(49) 评论(0)
编辑
自己写的一个批处理文件用于windows服务的安装管理
@echo off
:manager
echo 1=安装,2=开始,3=卸载,4=挂起
set /p para=
if %para%==1 goto install
if %para%==2 sc start 读取队列
if %para%==3 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u bin\Debug\EmailService.exe
if %para%==4 sc stop 读取队列
pause
goto end
:install
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe bin\Debug\EmailService.exe
sc start 读取队列
pause
goto end
posted @ 2011-07-19 15:01 chenleinet 阅读(55) 评论(0)
编辑
自己写的一个批处理文件用于windows服务的安装管理
@echo off
:manager
echo 1=安装,2=开始,3=卸载,4=挂起
set /p para=
if %para%==1 goto install
if %para%==2 sc start 读取队列
if %para%==3 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u bin\Debug\EmailService.exe
if %para%==4 sc stop 读取队列
pause
goto end
:install
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe bin\Debug\EmailService.exe
sc start 读取队列
pause
goto end
posted @ 2011-07-19 15:00 chenleinet 阅读(44) 评论(0)
编辑
换了新公司,有了新的开始。
现在开始用消息队列,做邮件的异步发送。
本系统,通过webservice访问本地消息队列,没有用wcf的消息队列访问,也没有用公共队列。
不过由于有webservice的支持,访问本地消息队列,没有任何的问题。
以下为队列操作代码
/// <summary>
/// 队列操作类
/// </summary>
public class OperateQueue:Interface.IStore
{
/// <summary>
/// 建立队列
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>是否成功</returns>
private ResultInfo<bool> CreateQueue(string queueName)
{
ResultInfo<bool> result = new ResultInfo<bool>();
result.Data = false;
result.Result = false;
try
{
MessageQueue.Create(queueName, true);
result.Data = true;
result.Result = true;
}
catch(Exception ex)
{
throw ex;
}
return result;
}
/// <summary>
/// 判断队列是否存在
/// </summary>
/// <param name="queueName">队列名字</param>
/// <returns></returns>
private bool IsQueueExists(string queueName)
{
return MessageQueue.Exists(queueName);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>是否成功</returns>
public ResultInfo<bool> StoreMessage(string queueName, string message)
{
if (!this.IsQueueExists(queueName))
{
this.CreateQueue(queueName);
}
MessageQueue queue = new MessageQueue() ;
ResultInfo<bool> result = new ResultInfo<bool>();
result.Data = false;
result.Result = false;
MessageQueueTransaction mts = new MessageQueueTransaction();
try
{
mts.Begin();
queue= new MessageQueue(queueName);
queue.Send(message,mts);
result.Data = true;
result.Message = "消息存储成功";
result.Result = true;
mts.Commit();
}
catch (Exception ex)
{
mts.Abort();
throw ex;
}
finally
{
queue.Close();
queue.Dispose();
}
return result;
}
/// <summary>
/// 读取消息
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>读取到的消息</returns>
public ResultInfo<string> ReadMessage(string queueName)
{
ResultInfo<string> result = new ResultInfo<string>();
result.Data = string.Empty;
result.Result = false;
MessageQueue queue = new MessageQueue();
try
{
queue = new MessageQueue(queueName);
Message message = new Message();
message = queue.Receive();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
result.Data = message.Body.ToString();
result.Result = true;
}
catch(Exception ex)
{
result.Message = ex.Message;
}
finally
{
queue.Close();
queue.Dispose();
}
return result;
}
}
采用了队列的事务操作,istore用来与上层调用进行隔离,这样做是为了保证,当需要使用其它消息队列时,做到最小改动。
webservice和队列部署在同一台服务器上,本打算利用
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(@"FormatName:DIRECT=tcp:192.168.16.85\private$\Action");
去进行局域网内的访问,但是没有成功,所以暂时放弃。
posted @ 2011-07-19 14:56 chenleinet 阅读(32) 评论(0)
编辑