Parsing web.config with XmlDocument.SelectSingleNode
2013-08-28 21:06
459 查看
问题现象:
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
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
相关文章推荐
- C#操作XML文档(XmlDocument、XmlNode、XmlAttribute、SelectSingleNode、SelectNodes、XmlNodeList)
- XmlDocument.SelectSingleNode and xmlNamespace issue
- C#操作XML文档(XmlDocument、XmlNode、XmlAttribute、SelectSingleNode、SelectNodes、XmlNodeList)
- XmlDocument的selectSingleNode方法,总支持Xpath函数吗?
- C#操作XML文档(XmlDocument、XmlNode、XmlAttribute、SelectSingleNode、SelectNodes、XmlNodeList)
- C#操作XML文档(XmlDocument、XmlNode、XmlAttribute、SelectSingleNode、SelectNodes、XmlNodeList)
- Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/config/springdemo-config.xml]
- C#操作XML文档(XmlDocument、XmlNode、XmlAttribute、SelectSingleNode、SelectNodes、XmlNodeList)
- XmlDocument SelectSingleNode
- C#操作XML文档(XmlDocument、XmlNode、XmlAttribute、SelectSingleNode、SelectNodes、XmlNodeList)
- Xml中SelectSingleNode方法中的xpath用法
- 读取xml中的SelectSingleNode方法
- Xml中SelectSingleNode方法中的xpath用法
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL原因和解决方案
- javax.servlet.UnavailableException: Parsing error processing resource path /WEB-INF/struts-config.xml
- Xml中SelectSingleNode方法中的xpath用法
- 异常: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContex
- Parsing error processing resource path /WEB-INF/struts-config.xml解决办法
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
- Xml中SelectSingleNode方法中的xpath用法