惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Martin Fowler
Martin Fowler
博客园_首页
Stack Overflow Blog
Stack Overflow Blog
Last Week in AI
Last Week in AI
The GitHub Blog
The GitHub Blog
B
Blog
C
Check Point Blog
WordPress大学
WordPress大学
G
Google Developers Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
量子位
月光博客
月光博客
U
Unit 42
Engineering at Meta
Engineering at Meta
有赞技术团队
有赞技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 【当耐特】
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Jina AI
Jina AI
S
Secure Thoughts
aimingoo的专栏
aimingoo的专栏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
Intezer
Latest news
Latest news
V
Vulnerabilities – Threatpost
D
Docker
Attack and Defense Labs
Attack and Defense Labs
Help Net Security
Help Net Security
S
Security @ Cisco Blogs
Forbes - Security
Forbes - Security
MongoDB | Blog
MongoDB | Blog
云风的 BLOG
云风的 BLOG
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
Cloudbric
Cloudbric
Spread Privacy
Spread Privacy

博客园 - DingJun

Javascript 继承方法3 Javascript 继承方法2 Javascript 继承方法1 Call web service from excel Cannot load type (加载页面出错) 几个主流的浏览器引擎及判定 防止数据库日志文件增长 配置发布数据库服务器时碰到错误18483 一些有关。NET界面处理与多线程的文章 不可恢复的生成错误 在.Net安装项目中如何判断操作系统的版本 修改dataConfiguration.config文件 SQL Server 使用外部连接 在.NET下利用目录服务操纵本机用户和用户组 System.windows.forms.datagrid控件使用技巧 读取配置文件中的自定义节 区域设置与格式化(1) 默认的 IIS MIME 类型关联 使用Data access block
在.NET中使用XPath查找指定元素时遇到的麻烦(以dataConfiguration.config为例)
DingJun · 2005-07-21 · via 博客园 - DingJun

XPath提供了一种方便的途径来供我们查找特定的XML元素或结点,以如下的XML片段为例:


1,查找所有book元素://book
2,查找id="bk104"的book元素://book[@id="bk104"]
3,查找书名为Midnight Rain的book元素://book[title="Midnight Rain"]
4,查找值为44.95的price元素://book/price[.="44.95"]
5,查找值为"bk104"的id属性://book/@id[.="bk104"]

在.NET下使用XPath也非常方便,见如下示例代码:
XmlDocument doc = new XmlDocument();
doc.Load("book.xml");

XmlNode price = doc.SelectSingleNode("//book/price[.="44.95"]")

但前两天在使用XPath从dataConfiguration.config文件中获取数据库连接串时遇到了不少麻烦,dataConfiguration.config文件内容如下:

现在要从此文件中获取parameter[@name='database']的元素,使用如上的示例代码却不能找到想要的parameter元素。经过长时间的摸索调查,终于发现问题出在enterpriseLibrary.databaseSettings元素上,刚开始以为是该元素中间的“."所致,后经验证在元素中出现“."不会引起查找问题,最后发现该元素后附加了几个名称空间,问题就出在这里,在元素中有如下的名称空间:xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data",这句话就暗示该元素的默认名称是:http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data,该元素及其所有该元素的子元素都属该名称空间管辖,该元素及其所有子元素的全称就是:名称空间:元素名称。所以在查找的XPath语句中只指定元素名称而不指定名称空间前缀是不能找到特定元素的。正确的代码如下: