c#通过xpath读取xml示例
需要修改main方法第一行代码的路径为你的books.xml文件绝对路径或相对路径。代码演示了xpath各种语法的使用情况
books.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<bookstore>
<book category="cooking">
<title lang="en">everyday italian</title>
<author>giada de laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">harry potter</title>
<author>j k. rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">xquery kick start</title>
<author>james mcgovern</author>
<author>per bothner</author>
<author>kurt cagle</author>
<author>james linn</author>
<author>vaidyanathan nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web">
<title lang="en">learning xml</title>
<author>erik t. ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
主程序
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.xml;
namespace xmlprocesstest
{
public class program
{
/// <summary>
/// 加载xml文件
/// </summary>
/// <param name="xmlfilepath">xml文件路径</param>
/// <returns></returns>
public static xmldocument loadxmldoc(string xmlfilepath)
{
var xmldoc = new xmldocument();
xmldoc.load(xmlfilepath);
return xmldoc;
}
/// <summary>
/// 根据指定的xpath表达式获取xml结点列表
/// </summary>
/// <param name="xmldoc"></param>
/// <param name="xpathexpr"></param>
/// <returns></returns>
public static xmlnodelist getxmlnodes(xmldocument xmldoc, string xpathexpr)
{
if (xmldoc == null)
return null;
return xmldoc.selectnodes(xpathexpr);
}
public static string getxmlnodeinfo(xmlnode node, string type="xml")
{
if (node == null)
return "empty node or error node";
string xmlnodeinfo = null;
switch (type)
{
case "text":
xmlnodeinfo = node.innertext;
break;
default:
xmlnodeinfo = node.innerxml;
break;
}
return xmlnodeinfo;
}
public static void main(string[] args)
{
var xmldoc = loadxmldoc(@"你的books.xml文件路径");
var rootexpr = "/bookstore"; // 根节点对应的xpath表达式
var rootnode = getxmlnodes(xmldoc, rootexpr); //
console.writeline("xpath表达式为 /bookstore,根节点bookstore的所有子节点xml内容如下:");
console.writeline(getxmlnodeinfo(rootnode[0]));
console.writeline();
var allbooksexpr = "/bookstore/book"; // 根节点bookstore的子元素的所有子节点
var booknodes = getxmlnodes(xmldoc, allbooksexpr);
console.writeline("xpath表达式为 bookstore/book,book节点共有:" + booknodes.count);
console.writeline();
var anybookexpr = "//book"; // 选取所有book子元素,而不管它们在文档中的位置
var anybooknodes = getxmlnodes(xmldoc, anybookexpr);
console.writeline("xpath表达式为 //book,book节点共有:" + anybooknodes.count);
console.writeline(anybooknodes[0].innerxml);
console.writeline(anybooknodes[0].outerxml);
console.writeline();
var categoryexpr = "//@category"; // 选取名为category的所有属性
var allcategorynodes = getxmlnodes(xmldoc, categoryexpr);
console.writeline("xpath表达式为 //@category,category节点共有:" + allcategorynodes.count);
console.writeline(allcategorynodes[0].innertext);
console.writeline(allcategorynodes[0].innerxml);
console.writeline();
var titlewithlangexpr = "//title[@lang]"; // 选取所有带有lang属性的title节点
var titlewithlangnodes = getxmlnodes(xmldoc, titlewithlangexpr);
console.writeline("xpath表达式为 //title[@lang],带lang属性的title节点共有:" + titlewithlangnodes.count);
console.writeline(getxmlnodeinfo(titlewithlangnodes[0]));
var englishtitleexpr = "//title[@lang='en']"; // 选取所有lang属性值为en的title节点
var englishtitlenodes = getxmlnodes(xmldoc, englishtitleexpr);
console.writeline("xpath表达式为 //title[@lang='en'],lang属性值为en的title节点共有:" + englishtitlenodes.count);
console.writeline(getxmlnodeinfo(englishtitlenodes[0]));
console.writeline();
// 使用索引的xpath查询
var indexexpr = "/bookstore/book[1]"; // 取bookstore子元素的第一个book元素
var firstbooknode = getxmlnodes(xmldoc, indexexpr);
console.writeline("xpath表达式为 /bookstore/book[1],节点数为:" + firstbooknode.count);
console.writeline(getxmlnodeinfo(firstbooknode[0]));
console.writeline();
var indexexpr2 = "/bookstore/book[last()]"; // 取bookstore子元素的最后一个book元素
var lastbooknode = getxmlnodes(xmldoc, indexexpr2);
console.writeline("xpath表达式为 /bookstore/book[last()],节点数为:" + lastbooknode.count);
console.writeline(getxmlnodeinfo(lastbooknode[0]));
console.writeline();
var indexexpr3 = "/bookstore/book[last()-1]"; // 取bookstore子元素的倒数第二个book元素
var nextbylastbooknode = getxmlnodes(xmldoc, indexexpr3);
console.writeline("xpath表达式为 /bookstore/book[last()-1],节点数为:" + lastbooknode.count);
console.writeline(getxmlnodeinfo(nextbylastbooknode[0]));
console.writeline();
var indexexpr4 = "/bookstore/book[position()<3]"; // 取bookstore的前两个book子元素
var firsttwobooknodes = getxmlnodes(xmldoc, indexexpr4);
console.writeline("xpath表达式为 /bookstore/book[position()<3],节点数为:" + firsttwobooknodes.count);
console.writeline(getxmlnodeinfo(firsttwobooknodes[0]));
console.writeline();
// 带属性值过滤条件的xpath表达式
var fileterexpr = "/bookstore/book[price>35.00]"; // 选取bookstore的所有price属性值大于35.00的book元素
var bookgt35nodes = getxmlnodes(xmldoc, fileterexpr);
console.writeline("xpath表达式为 /bookstore/book[price>35.00],节点数为:" + bookgt35nodes.count);
console.writeline(getxmlnodeinfo(bookgt35nodes[0]));
// 通配符
// @* 匹配任何属性节点
// node() 匹配任何类型的节点
// /bookstore/* 选取 bookstore 元素的所有子元素
// //* 选取文档的所有元素
// //title[@*] 选取所有带有属性的 title 元素
var alltitlewithattrexpr = "//title[@*]";
var alltitlewithattrnodes = getxmlnodes(xmldoc, alltitlewithattrexpr);
console.writeline("xpath表达式为 title[@*],节点数为:" + alltitlewithattrnodes.count);
console.writeline(getxmlnodeinfo(alltitlewithattrnodes[0]));
console.writeline();
// | 或
var titleandpriceexpr = "//book/title | //book/price";
var titleandpricenodes = getxmlnodes(xmldoc, titleandpriceexpr);
console.writeline("xpath表达式为 //book/title | //book/price,节点数为:" + titleandpricenodes.count);
console.writeline(getxmlnodeinfo(titleandpricenodes[0]));
// text() 选取文本
var titletextexpr = "//title/text()";
var titletextnodes = getxmlnodes(xmldoc, titletextexpr);
console.writeline("xpath表达式为 //title/text(),节点数为:" + titletextnodes.count);
console.writeline(titletextnodes[0].value); // 文本节点的值
console.readkey();
}
}
}
上一篇: RT-THread timer