问题
xml
<sqlMap namespace="WHTR.Dao.Accounts" xmlns="http://ibatis.apache.org/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </sqlMap>
这种带有 xmlns 属性的文档如果使用正常的 xpath 语法是获取不到元素或属性的。
正确的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml; namespace CSharpStudy
{
class Program
{
static void Main(string[] args)
{
var xmlDocument = new XmlDocument();
xmlDocument.Load(@"E:\段光伟\代码\摇钱树\PPmoney_New\Libraries\WHTR.Dao\Accounts\Impls\SqlMap\SuperviseAccount.xml");
var nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
nsmgr.AddNamespace("b", "http://ibatis.apache.org/mapping");
var nodes = xmlDocument.SelectNodes("//b:select", nsmgr);
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.Attributes["id"].Value.Split('.').Last());
}
}
}
}
参考网站
http://codeclimber.net.nz/archive/2008/01/09/How-to-query-a-XPath-doc-that-has-a-default.aspx
http://stackoverflow.com/questions/2075773/xpath-for-xml-with-namespace