Parsing web.config with XmlDocument.SelectSingleNode

本文讨论了在使用ASP.NET时遇到的配置文件中XML命名空间导致SelectSingleNode无法正常工作的问题,并提供了使用XmlNamespaceManager的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题现象:

 

I have an utility that modified ASP.NET web.config using code like this:
 

XmlDocument document = new XmlDocument();
document.Load(sConfigFileName);
XmlNode nodeParent = document.SelectSingleNode("/configuration/system.web");
 
I found that this code doesn't work with VS 2005 (SelectSingleNode returns null) because configuration element has xmlns
 
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> .
 
I was able to delete xmlns attribute without any visible side effects to make SelectSingleNode("/configuration/system.web") working .
 
But it will be probably required to use new classes in Configuration namespace or use XmlNamespaceManager.

 

 

解决方案:

 

XmlDocument document = new XmlDocument();
document.Load(sConfigFileName);
XmlNamespaceManager ns = new XmlNamespaceManager(document.NameTable);

ns.AddNamespace("x", "http://schemas.microsoft.com/.NetConfiguration/v2.0");

XmlNode myNode = document.SelectSingleNode("/x:configuration/x:system.web",ns);

 

试过可行

 

原文地址:http://geekswithblogs.net/mnf/archive/2006/02/02/67909.aspx

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值