c#通過xpath讀取xml示例
需要修改Main方法第一行代碼的路徑為你的books.xml文件絕對(duì)路徑或相對(duì)路徑。代碼演示了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>
/// 根據(jù)指定的XPath表達(dá)式獲取XML結(jié)點(diǎn)列表
/// </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"; // 根節(jié)點(diǎn)對(duì)應(yīng)的XPath表達(dá)式
var rootNode = GetXmlNodes(xmlDoc, rootExpr); //
Console.WriteLine("XPath表達(dá)式為 /bookstore,根節(jié)點(diǎn)bookstore的所有子節(jié)點(diǎn)XML內(nèi)容如下:");
Console.WriteLine(GetXmlNodeInfo(rootNode[0]));
Console.WriteLine();
var allBooksExpr = "/bookstore/book"; // 根節(jié)點(diǎn)bookstore的子元素的所有子節(jié)點(diǎn)
var bookNodes = GetXmlNodes(xmlDoc, allBooksExpr);
Console.WriteLine("XPath表達(dá)式為 bookstore/book,book節(jié)點(diǎn)共有:" + bookNodes.Count);
Console.WriteLine();
var anyBookExpr = "http://book"; // 選取所有book子元素,而不管它們?cè)谖臋n中的位置
var anyBookNodes = GetXmlNodes(xmlDoc, anyBookExpr);
Console.WriteLine("XPath表達(dá)式為 //book,book節(jié)點(diǎn)共有:" + anyBookNodes.Count);
Console.WriteLine(anyBookNodes[0].InnerXml);
Console.WriteLine(anyBookNodes[0].OuterXml);
Console.WriteLine();
var categoryExpr = "http://@category"; // 選取名為category的所有屬性
var allCategoryNodes = GetXmlNodes(xmlDoc, categoryExpr);
Console.WriteLine("XPath表達(dá)式為 //@category,category節(jié)點(diǎn)共有:" + allCategoryNodes.Count);
Console.WriteLine(allCategoryNodes[0].InnerText);
Console.WriteLine(allCategoryNodes[0].InnerXml);
Console.WriteLine();
var titleWithLangExpr = "http://title[@lang]"; // 選取所有帶有l(wèi)ang屬性的title節(jié)點(diǎn)
var titleWithLangNodes = GetXmlNodes(xmlDoc, titleWithLangExpr);
Console.WriteLine("XPath表達(dá)式為 //title[@lang],帶lang屬性的title節(jié)點(diǎn)共有:" + titleWithLangNodes.Count);
Console.WriteLine(GetXmlNodeInfo(titleWithLangNodes[0]));
var englishTitleExpr = "http://title[@lang='en']"; // 選取所有l(wèi)ang屬性值為en的title節(jié)點(diǎn)
var englishTitleNodes = GetXmlNodes(xmlDoc, englishTitleExpr);
Console.WriteLine("XPath表達(dá)式為 //title[@lang='en'],lang屬性值為en的title節(jié)點(diǎn)共有:" + englishTitleNodes.Count);
Console.WriteLine(GetXmlNodeInfo(englishTitleNodes[0]));
Console.WriteLine();
// 使用索引的XPath查詢
var indexExpr = "/bookstore/book[1]"; // 取bookstore子元素的第一個(gè)book元素
var firstBookNode = GetXmlNodes(xmlDoc, indexExpr);
Console.WriteLine("XPath表達(dá)式為 /bookstore/book[1],節(jié)點(diǎn)數(shù)為:" + firstBookNode.Count);
Console.WriteLine(GetXmlNodeInfo(firstBookNode[0]));
Console.WriteLine();
var indexExpr2 = "/bookstore/book[last()]"; // 取bookstore子元素的最后一個(gè)book元素
var lastBookNode = GetXmlNodes(xmlDoc, indexExpr2);
Console.WriteLine("XPath表達(dá)式為 /bookstore/book[last()],節(jié)點(diǎn)數(shù)為:" + lastBookNode.Count);
Console.WriteLine(GetXmlNodeInfo(lastBookNode[0]));
Console.WriteLine();
var indexExpr3 = "/bookstore/book[last()-1]"; // 取bookstore子元素的倒數(shù)第二個(gè)book元素
var nextByLastBookNode = GetXmlNodes(xmlDoc, indexExpr3);
Console.WriteLine("XPath表達(dá)式為 /bookstore/book[last()-1],節(jié)點(diǎn)數(shù)為:" + lastBookNode.Count);
Console.WriteLine(GetXmlNodeInfo(nextByLastBookNode[0]));
Console.WriteLine();
var indexExpr4 = "/bookstore/book[position()<3]"; // 取bookstore的前兩個(gè)book子元素
var firstTwoBookNodes = GetXmlNodes(xmlDoc, indexExpr4);
Console.WriteLine("XPath表達(dá)式為 /bookstore/book[position()<3],節(jié)點(diǎn)數(shù)為:" + firstTwoBookNodes.Count);
Console.WriteLine(GetXmlNodeInfo(firstTwoBookNodes[0]));
Console.WriteLine();
// 帶屬性值過濾條件的XPath表達(dá)式
var fileterExpr = "/bookstore/book[price>35.00]"; // 選取bookstore的所有price屬性值大于35.00的book元素
var bookGt35Nodes = GetXmlNodes(xmlDoc, fileterExpr);
Console.WriteLine("XPath表達(dá)式為 /bookstore/book[price>35.00],節(jié)點(diǎn)數(shù)為:" + bookGt35Nodes.Count);
Console.WriteLine(GetXmlNodeInfo(bookGt35Nodes[0]));
// 通配符
// @* 匹配任何屬性節(jié)點(diǎn)
// node() 匹配任何類型的節(jié)點(diǎn)
// /bookstore/* 選取 bookstore 元素的所有子元素
// //* 選取文檔的所有元素
// //title[@*] 選取所有帶有屬性的 title 元素
var allTitleWithAttrExpr = "http://title[@*]";
var allTitleWithAttrNodes = GetXmlNodes(xmlDoc, allTitleWithAttrExpr);
Console.WriteLine("XPath表達(dá)式為 title[@*],節(jié)點(diǎn)數(shù)為:" + allTitleWithAttrNodes.Count);
Console.WriteLine(GetXmlNodeInfo(allTitleWithAttrNodes[0]));
Console.WriteLine();
// | 或
var titleAndPriceExpr = "http://book/title | //book/price";
var titleAndPriceNodes = GetXmlNodes(xmlDoc, titleAndPriceExpr);
Console.WriteLine("XPath表達(dá)式為 //book/title | //book/price,節(jié)點(diǎn)數(shù)為:" + titleAndPriceNodes.Count);
Console.WriteLine(GetXmlNodeInfo(titleAndPriceNodes[0]));
// text() 選取文本
var titleTextExpr = "http://title/text()";
var titleTextNodes = GetXmlNodes(xmlDoc, titleTextExpr);
Console.WriteLine("XPath表達(dá)式為 //title/text(),節(jié)點(diǎn)數(shù)為:" + titleTextNodes.Count);
Console.WriteLine(titleTextNodes[0].Value); // 文本節(jié)點(diǎn)的值
Console.ReadKey();
}
}
}
相關(guān)文章
C#執(zhí)行存儲(chǔ)過程并將結(jié)果填充到GridView的方法
這篇文章主要介紹了C#執(zhí)行存儲(chǔ)過程并將結(jié)果填充到GridView的方法,結(jié)合實(shí)例形式分析了C#存儲(chǔ)過程操作及GridView控件相關(guān)操作技巧,需要的朋友可以參考下2017-02-02C#遞歸實(shí)現(xiàn)將一整數(shù)逆序后放入一數(shù)組中
這篇文章主要介紹了C#遞歸實(shí)現(xiàn)將一整數(shù)逆序后放入一數(shù)組中,是遞歸算法的一個(gè)簡(jiǎn)單應(yīng)用,需要的朋友可以參考下2014-10-10C# WinForm程序設(shè)計(jì)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C# WinForm程序設(shè)計(jì)簡(jiǎn)單計(jì)算器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02