XPath提供了一种方便的途径来供我们查找特定的XML元素或结点,以如下的XML片段为例:
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.</description>
</book>
</catalog>
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文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<dataConfiguration>
<xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<enterpriseLibrary.databaseSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defaultInstance="InstanceWebinpuy" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data">
<databaseTypes>
<databaseType name="Sql Server" type="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</databaseTypes>
<instances>
<instance name="InstanceWebinpuy" type="Sql Server" connectionString="Sql Connection String" />
</instances>
<connectionStrings>
<connectionString name="Sql Connection String">
<parameters>
<parameter name="database" value="BMS_Webinput" isSensitive="false" />
<parameter name="Integrated Security" value="False" isSensitive="false" />
<parameter name="server" value="192.168.1.28" isSensitive="false" />
</parameters>
</connectionString>
</connectionStrings>
</enterpriseLibrary.databaseSettings>
</xmlSerializerSection>
</dataConfiguration>现在要从此文件中获取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语句中只指定元素名称而不指定名称空间前缀是不能找到特定元素的。正确的代码如下:
XmlDocument doc = new XmlDocument();
doc.Load("dataConfiguration.config");
//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("MDAB", "http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data");
XmlNode nodeServer = doc.SelectSingleNode("//MDAB:parameters/MDAB:parameter[@name='server']", nsmgr);
if(nodeServer != null && nodeServer.Attributes["value"]!=null)
m_DbServer = nodeServer.Attributes["value"].Value;
浙公网安备 33010602011771号