首页 新闻 赞助 找找看

XML 文件加载问题

0
悬赏园豆:30 [已解决问题] 解决于 2016-06-04 16:32

代码如下,如果通过xml获取的路径,就提示我找不到路径,如果直接写死的话能正常运行。

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             FileSystemWatcher Watchaer = new FileSystemWatcher();
 6             Watchaer.IncludeSubdirectories = false;
 7             XmlDocument doc = new XmlDocument();
 8 
 9             doc.Load("E:\\source code\\E-Cargomonitor\\config.xml");
10             XmlElement rootelement = doc.DocumentElement;
11             XmlNodeList monitorpathnodes = rootelement.GetElementsByTagName("monitorpath");
12             XmlNode node = monitorpathnodes.Item(0);
13             string monitorpath = node.InnerText.ToString();
14             Console.Write(monitorpath);
15             Console.ReadLine();
16             //发生错误,中断,monitorpath的值是D:\barcode file,提示Additional information: The directory name @D:\barcode file is invalid.
17             Watchaer.Path ="@"+monitorpath;
18             //可正常运行 
19             //Watchaer.Path = @"D:\barcode file";
20 
21             Console.WriteLine(Watchaer.Path);
22             Console.ReadLine();
23         }
24     }
xuelang1225的主页 xuelang1225 | 初学一级 | 园豆:83
提问于:2014-05-12 13:08
< >
分享
最佳答案
0

monitorpath 中的“\” 改写成“\\” 这样就可以了

如果不想改xml中的内容也可以读出来后 把 “\” 替换成 "\\"

@不是写到字符串里面就可以用的,他是编译的时候告诉编译器,他后面的字符串是不用转译符的意思

收获园豆:30
刘宏玺 | 专家六级 |园豆:14020 | 2014-05-12 13:45

换成\\还是不行,我的xml文件如下

 

<?xml version="1.0" encoding="gb2312"?>
<root>
<monitorpath>
D:\\barcode file
</monitorpath>
<PICemail>
fusheng@fxcps.com.cn
</PICemail>
<NgPath>
D:\\barcode file_NG\
</NgPath>
<OKPath>
D:\\barcode file_OK\
</OKPath>
<backupPath>
D:\\E_cargo file
</backupPath>

<logpath>

</logpath>
</root>

无论\\还是\都是不行的,网上有一种说法让我改第一行的编码方式,我修改了仍然不行!!

xuelang1225 | 园豆:83 (初学一级) | 2014-05-12 13:56

@xuelang1225: D:\\barcode file_NG\  这个地方的后面为啥还是“\”,下面的也是,你试试都改成“\\”

刘宏玺 | 园豆:14020 (专家六级) | 2014-05-12 13:59

@博客¥: 下面不影响的,我只用到D:\\barcode file这个节点,其它的在代码里完全没有使用的,我换了试一试还是一样的错误,现在的问题是调试时候变量monitorpath的值和直接写死的值时一样的,但是执行Watchaer.Path ="@"+monitorpath;这个语句就出错

xuelang1225 | 园豆:83 (初学一级) | 2014-05-12 14:22

@博客¥: 谢谢搞定了,monitorpath.Trim();空格的问题!

xuelang1225 | 园豆:83 (初学一级) | 2014-05-12 14:36
其他回答(1)
0

这个Console.ReadLine();执行完后吗?

sunlary | 园豆:934 (小虾三级) | 2014-05-12 13:19

执行到Watchaer.Path ="@"+monitorpath;时候中断了,但是Watchaer.Path 和直接写死时候值时一样的。Watchaer.Path = @"D:\barcode file";直接写死路径的代码。

支持(0) 反对(0) xuelang1225 | 园豆:83 (初学一级) | 2014-05-12 13:31

@xuelang1225: 有时候上面使用过就被占用了,不释放下面的用不了。

支持(0) 反对(0) sunlary | 园豆:934 (小虾三级) | 2014-05-12 14:13

@sunlary: 谢谢搞定了,monitorpath.Trim();空格的问题!

支持(0) 反对(0) xuelang1225 | 园豆:83 (初学一级) | 2014-05-12 14:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册