C# · 12月 26, 2021

c#通过xpath读取xml示例

需要修改Main方法第一行代码的路径为你的books.xml文件绝对路径或相对路径。代码演示了XPath各种语法的使用情况

books.xml @H_403_3@复制代码 代码如下:@H_403_3@<?xml version=”1.0″ encoding=”ISO-8859-1″?>@H_403_3@<bookstore>

<book category=”COOKING”>@H_403_3@ <title lang=”en”>Everyday Italian</title>@H_403_3@ <author>Giada De Laurentiis</author>@H_403_3@ <year>2005</year>@H_403_3@ <price>30.00</price>@H_403_3@ </book>

<book category=”CHILDREN”>@H_403_3@ <title lang=”en”>Harry Potter</title>@H_403_3@ <author>J K. Rowling</author>@H_403_3@ <year>2005</year>@H_403_3@ <price>29.99</price>@H_403_3@ </book>

<book category=”WEB”>@H_403_3@ <title lang=”en”>XQuery Kick Start</title>@H_403_3@ <author>James McGovern</author>@H_403_3@ <author>Per Bothner</author>@H_403_3@ <author>Kurt Cagle</author>@H_403_3@ <author>James Linn</author>@H_403_3@ <author>Vaidyanathan Nagarajan</author>@H_403_3@ <year>2003</year>@H_403_3@ <price>49.99</price>@H_403_3@ </book>

<book category=”WEB”>@H_403_3@ <title lang=”en”>Learning XML</title>@H_403_3@ <author>Erik T. Ray</author>@H_403_3@ <year>2003</year>@H_403_3@ <price>39.95</price>@H_403_3@ </book>

</bookstore>@H_403_3@

主程序@H_403_3@复制代码 代码如下:@H_403_3@using System;@H_403_3@using System.Collections.Generic;@H_403_3@using System.Linq;@H_403_3@using System.Text;@H_403_3@using System.Threading.Tasks;@H_403_3@using System.Xml;

namespace XmlProcessTest@H_403_3@{@H_403_3@ public class Program@H_403_3@ {@H_403_3@ /// <summary>@H_403_3@ /// 加载XML文件@H_403_3@ /// </summary>@H_403_3@ /// <param name=”xmlFilePath”>XML文件路径</param>@H_403_3@ /// <returns></returns>@H_403_3@ public static XmlDocument LoadXmlDoc(string xmlFilePath)@H_403_3@ {@H_403_3@ var xmlDoc = new XmlDocument();@H_403_3@ xmlDoc.Load(xmlFilePath);

return xmlDoc;@H_403_3@ }

/// <summary>@H_403_3@ /// 根据指定的XPath表达式获取XML结点列表@H_403_3@ /// </summary>@H_403_3@ /// <param name=”xmlDoc”></param>@H_403_3@ /// <param name=”xpathExpr”></param>@H_403_3@ /// <returns></returns>@H_403_3@ public static XmlNodeList GetXmlNodes(XmlDocument xmlDoc,string xpathExpr)@H_403_3@ {@H_403_3@ if (xmlDoc == null)@H_403_3@ return null;

return xmlDoc.SelectNodes(xpathExpr);@H_403_3@ }

public static string GetXmlNodeInfo(XmlNode node,string type=”xml”)@H_403_3@ {@H_403_3@ if (node == null)@H_403_3@ return “Empty node or error node”;

string xmlNodeInfo = null;@H_403_3@ switch (type)@H_403_3@ {@H_403_3@ case “text”:@H_403_3@ xmlNodeInfo = node.InnerText;@H_403_3@ break;@H_403_3@ default:@H_403_3@ xmlNodeInfo = node.InnerXml;@H_403_3@ break;@H_403_3@ }

return xmlNodeInfo;@H_403_3@ }

public static void Main(string[] args)@H_403_3@ {@H_403_3@ var xmlDoc = LoadXmlDoc(@”你的books.xml文件路径”);

var rootExpr = “/bookstore”; // 根节点对应的XPath表达式@H_403_3@ var rootNode = GetXmlNodes(xmlDoc,rootExpr); // @H_403_3@ Console.WriteLine(“XPath表达式为 /bookstore,根节点bookstore的所有子节点XML内容如下:”);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(rootNode[0]));

Console.WriteLine();

var allBooksExpr = “/bookstore/book”; // 根节点bookstore的子元素的所有子节点@H_403_3@ var bookNodes = GetXmlNodes(xmlDoc,allBooksExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 bookstore/book,book节点共有:” + bookNodes.Count);

Console.WriteLine();

var anyBookExpr = “//book”; // 选取所有book子元素,而不管它们在文档中的位置@H_403_3@ var anyBookNodes = GetXmlNodes(xmlDoc,anyBookExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 //book,book节点共有:” + anyBookNodes.Count);@H_403_3@ Console.WriteLine(anyBookNodes[0].InnerXml);@H_403_3@ Console.WriteLine(anyBookNodes[0].OuterXml);

Console.WriteLine();

var categoryExpr = “//@category”; // 选取名为category的所有属性@H_403_3@ var allCategoryNodes = GetXmlNodes(xmlDoc,categoryExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 //@category,category节点共有:” + allCategoryNodes.Count);@H_403_3@ Console.WriteLine(allCategoryNodes[0].InnerText);@H_403_3@ Console.WriteLine(allCategoryNodes[0].InnerXml);

Console.WriteLine();

var titleWithLangExpr = “//title[@lang]”; // 选取所有带有lang属性的title节点@H_403_3@ var titleWithLangNodes = GetXmlNodes(xmlDoc,titleWithLangExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 //title[@lang],带lang属性的title节点共有:” + titleWithLangNodes.Count);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(titleWithLangNodes[0]));

var englishTitleExpr = “//title[@lang=’en’]”; // 选取所有lang属性值为en的title节点@H_403_3@ var englishTitleNodes = GetXmlNodes(xmlDoc,englishTitleExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 //title[@lang=’en’],lang属性值为en的title节点共有:” + englishTitleNodes.Count);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(englishTitleNodes[0]));

Console.WriteLine();

// 使用索引的XPath查询@H_403_3@ var indexExpr = “/bookstore/book[1]”; // 取bookstore子元素的第一个book元素@H_403_3@ var firstBookNode = GetXmlNodes(xmlDoc,indexExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 /bookstore/book[1],节点数为:” + firstBookNode.Count);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(firstBookNode[0]));

Console.WriteLine();

var indexExpr2 = “/bookstore/book[last()]”; // 取bookstore子元素的最后一个book元素@H_403_3@ var lastBookNode = GetXmlNodes(xmlDoc,indexExpr2);@H_403_3@ Console.WriteLine(“XPath表达式为 /bookstore/book[last()],节点数为:” + lastBookNode.Count);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(lastBookNode[0]));

Console.WriteLine();

var indexExpr3 = “/bookstore/book[last()-1]”; // 取bookstore子元素的倒数第二个book元素@H_403_3@ var nextByLastBookNode = GetXmlNodes(xmlDoc,indexExpr3);@H_403_3@ Console.WriteLine(“XPath表达式为 /bookstore/book[last()-1],节点数为:” + lastBookNode.Count);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(nextByLastBookNode[0]));

Console.WriteLine();

var indexExpr4 = “/bookstore/book[position()<3]”; // 取bookstore的前两个book子元素@H_403_3@ var firstTwoBookNodes = GetXmlNodes(xmlDoc,indexExpr4);@H_403_3@ Console.WriteLine(“XPath表达式为 /bookstore/book[position()<3],节点数为:” + firstTwoBookNodes.Count);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(firstTwoBookNodes[0]));

Console.WriteLine();

// 带属性值过滤条件的XPath表达式@H_403_3@ var fileterExpr = “/bookstore/book[price>35.00]”; // 选取bookstore的所有price属性值大于35.00的book元素@H_403_3@ var bookGt35Nodes = GetXmlNodes(xmlDoc,fileterExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 /bookstore/book[price>35.00],节点数为:” + bookGt35Nodes.Count);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(bookGt35Nodes[0]));

// 通配符@H_403_3@ // @* 匹配任何属性节点@H_403_3@ // node() 匹配任何类型的节点@H_403_3@ // /bookstore/* 选取 bookstore 元素的所有子元素@H_403_3@ // //* 选取文档的所有元素@H_403_3@ // //title[@*] 选取所有带有属性的 title 元素@H_403_3@ var allTitleWithAttrExpr = “//title[@*]”;@H_403_3@ var allTitleWithAttrNodes = GetXmlNodes(xmlDoc,allTitleWithAttrExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 title[@*],节点数为:” + allTitleWithAttrNodes.Count);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(allTitleWithAttrNodes[0]));

Console.WriteLine();

// | 或@H_403_3@ var titleAndPriceExpr = “//book/title | //book/price”;@H_403_3@ var titleAndPriceNodes = GetXmlNodes(xmlDoc,titleAndPriceExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 //book/title | //book/price,节点数为:” + titleAndPriceNodes.Count);@H_403_3@ Console.WriteLine(GetXmlNodeInfo(titleAndPriceNodes[0]));

// text() 选取文本@H_403_3@ var titleTextExpr = “//title/text()”;@H_403_3@ var titleTextNodes = GetXmlNodes(xmlDoc,titleTextExpr);@H_403_3@ Console.WriteLine(“XPath表达式为 //title/text(),节点数为:” + titleTextNodes.Count);@H_403_3@ Console.WriteLine(titleTextNodes[0].Value); // 文本节点的值

Console.ReadKey();@H_403_3@ }@H_403_3@ }@H_403_3@}@H_403_3@