using System;
using System.IO;
using System.Xml;class SetCFG
{
  static void Main(string[] args)
  {
if (args.Length < 2)
{
Console.WriteLine("usage: {0} filename node-path:newvalue", Environment.CommandLine.Split(' ')[0]);
return;
} string sFile = args[0];
string[] nodeParam = args[1].Split(':');
if (nodeParam.Length < 2)
{
Console.WriteLine("not enough information on the nodes");
return;
} if (! File.Exists(sFile))
{
Console.WriteLine("{0} doesn't exist", sFile);
return;
} try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(sFile);
XmlNode node = xmldoc.SelectSingleNode(nodeParam[0].Replace('|','/'));
if (node != null)
{
node.InnerText = nodeParam[1]; xmldoc.Save(sFile);
}
else
Console.WriteLine("node doesn't exist"); }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
  }
}
you need to call likeSetCFG Web.xml "/configuration|configSections:efg"since "|" has a special meaning in dos

解决方案 »

  1.   

    不对啊,怎么执行以后web。xml变成下面了?<?xml version="1.0" encoding="utf-8"?>
    <configuration>efg</configuration>
      

  2.   

    你命令行输入对了没有,因为在DOS里“|”是pipe的意思,你需要将XPath用括号括起来SetCFG Web.xml "/configuration|configSections:efg"
      

  3.   

    谢谢你的方法,我经过一定的修改就可以用了。但是现在有一个问题,xml里面的注释如何处理,
    现在如果xml文里有注释的话,这个程序就会出问题了。
    如果去掉注释就对了,请问怎么过滤呢?
      

  4.   

    我的程序应该是不需要修改即能执行的,假如你没变动XML文件或命令行指令的话>>>现在如果xml文里有注释的话,这个程序就会出问题了什么问题?你的注释在哪里?你的文件确定是完整的XML么?如果是中文的话你也许需要using System.IO;StreamReader reader = new StreamReader(sFile, System.Text.Encoding.GetEncoding("GB2312"));
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(reader);
    reader.Close();StreamWriter writer = new StreamWriter(sFile, false, System.Text.Encoding.GetEncoding("GB2312"));
    xmldoc.Save(writer);
    writer.Close();
      

  5.   

    呵呵,你的程序是可以用的,只是我加入了一些我自己的需求,呵呵。
    注释的问题如下:
    我的web.xml  文档如下:<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>abc</configSections>
    <!-- ToDo: this appSettings is copied from Mars -->
    <appSettings>
    <rag>liyouxin</rag>
    <datasource>sql.liyouxin.connect</datasource >
    <others>
    <name>test</name>
    <pwd>abc</pwd>
    </others>
    </appSettings>
    </configuration>
    如果<!-- ToDo: this appSettings is copied from Mars -->注释存在的话
    程序就在找节点的时候出错了,去掉就好了,
    怎么才能过滤掉注释呢?或者让他不起作用。
    我现在结帖,如果能帮这个忙,我新开30分的帖子,说话算数的。
    thanks