C#通过xpath查找xml指定元素的方法

本文实例讲述了C#通过xpath查找xml指定元素的方法。分享给大家供大家参考。具体如下:

orders.xml文档内容如下

<?xml version="1.0"?>
<Order id="2004-01-30.195496">
 <Client id="ROS-930252034">
 <Name>Remarkable Office Supplies</Name>
 </Client>
 <Items>
 <Item id="1001">
  <Name>Electronic Protractor</Name>
  <Price>42.99</Price>
 </Item>
 <Item id="1002">
  <Name>Invisible Ink</Name>
  <Price>200.25</Price>
 </Item>
 </Items>
</Order>

C#代码

using System;
using System.Xml;
public class XPathSelectNodes {
 private static void Main() {
  // Load the document.
  XmlDocument doc = new XmlDocument();
  doc.Load("orders.xml");
  XmlNodeList nodes = doc.SelectNodes("/Order/Items/Item/Name");
  foreach (XmlNode node in nodes) {
   Console.WriteLine(node.InnerText);
  }
  Console.ReadLine();
 }
}

输出结果

Electronic Protractor
Invisible Ink

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • c# 二分查找算法

    折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较. C 如果在某一步骤数组为空,则代表找不到.这种搜索算法每一次比较都使搜索范围缩小一半. 时间复杂度折半搜索每次把搜索区域减少一半,时间复杂度为. (n代表集合中元素的个数)空间复杂度 复制代码 代码如下: //

  • C#查找字符串所有排列组合的方法

    本文实例讲述了C#查找字符串所有排列组合的方法.分享给大家供大家参考.具体实现方法如下: // 1. remove first char // 2. find permutations of the rest of chars // 3. Attach the first char to each of those permutations. // 3.1 for each permutation, move firstChar in all indexes // to produce even

  • C# 数组查找与排序实现代码

    1. 查找对象 复制代码 代码如下: Person p1 = new Person( " http://www.my400800.cn " , 18 ); Person p2 = new Person( " http://www.my400800.cn " , 19 ); Person p3 = new Person( " http://www.my400800.cn " , 20 ); Person[] persons = ... { p1,

  • C#中查找Dictionary中重复值的方法

    简介 在这篇帮助文档中,我将向你展示如何实现c#里字典中重复值的查找.你知道的对于一个老鸟来说,这是非常简单的代码.但是尽管如此,这也是一篇对c#初学者非常有用的帮助文档. 背景 多数程序员对小型数据源存储的处理方式通常是创建字典进行键值存储.主键时唯一的,但是字典值却可能有重复的元素. 代码 这里我使用了一个简单的LINQ语句来查找字典中的重复值. 复制代码 代码如下: //initialize a dictionary with keys and values.    Dictionary<

  • C#查找列表中所有重复出现元素的方法

    本文实例讲述了C#查找列表中所有重复出现元素的方法.分享给大家供大家参考.具体实现方法如下: public T[] GetDuplicates(T inputValue) { List<T> duplicates = new List<T>( ); for (int i = 0; i < this.Count; i++) { if (this[i].Equals(inputValue)) { duplicates.Add(this[i]); } } return (dupli

  • C#查找素数实现方法

    本文所述为C#查找素数的程序代码,包括了可视化窗体的代码,找素数的方法可以借鉴.虽然实现的功能简单,不过为了演示一些C#技巧,本文实例中还用到了线程技术.ListBox列表框的使用.设置程序挂起等操作,其中备有详尽的注释,帮助大家更好的理解. 具体实现代码如下: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms;

  • C#中怎样从指定字符串中查找并替换字符串?

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;#region             #endregionnamespace Find{    public partial class Form

  • C#二分查找算法实例分析

    本文实例讲述了C#二分查找算法.分享给大家供大家参考.具体实现方法如下: // input array is assumed to be sorted public int BinarySearch(int[] arr, int x) { if (arr.Length == 0) return -1; int mid = arr.Length / 2; if (arr[mid] == x) return mid; if (x < arr[mid]) return BinarySearch(Get

  • C# 递归查找树状目录实现方法

    1.递归查找树状目录 复制代码 代码如下: public partial class Form1 : Form    {        string path = @"F:\学习文件";//递归查找树状目录        public Form1()        {递归查找树状目录            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e) 

  • C#通过xpath查找xml指定元素的方法

    本文实例讲述了C#通过xpath查找xml指定元素的方法.分享给大家供大家参考.具体如下: orders.xml文档内容如下 <?xml version="1.0"?> <Order id="2004-01-30.195496"> <Client id="ROS-930252034"> <Name>Remarkable Office Supplies</Name> </Client

  • JavaScript判断数组是否包含指定元素的方法

    本文实例讲述了JavaScript判断数组是否包含指定元素的方法.分享给大家供大家参考.具体如下: 这段代码通过prototype定义了数组方法,这样就可以在任意数组调用contains方法 /** * Array.prototype.[method name] allows you to define/overwrite an objects method * needle is the item you are searching for * this is a special variab

  • python实现判断数组是否包含指定元素的方法

    本文实例讲述了python实现判断数组是否包含指定元素的方法.分享给大家供大家参考.具体如下: python判断数组是否包含指定的元素的方法,直接使用in即可,python真是简单易懂 print 3 in [1, 2, 3] # membership (1 means true inventory = ["sword", "armor", "shield", "healing potion"] if "healin

  • jQuery经过一段时间自动隐藏指定元素的方法

    本文实例讲述了jQuery经过一段时间自动隐藏指定元素的方法.分享给大家供大家参考.具体如下: 下面的代码提供了两种方法用于定时隐藏元素,第一种是使用setTimeout实现,第二种是使用jQuery1.4以后版本中提供的delay方法实现,第二种更简单一下. //这是1.3.2中我们使用setTimeout来实现的方式 //http://www.jb51.net setTimeout(function() { $('.showdiv').hide('blind', {}, 500) }, 50

  • jQuery使用hide方法隐藏页面上指定元素的方法

    本文实例讲述了jQuery使用hide方法隐藏页面上指定元素的方法.分享给大家供大家参考.具体如下: 下面的JS代码隐藏了网页中所有<p>标签的内容 <!DOCTYPE html> <html> <head> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button&qu

  • 浅析jquery数组删除指定元素的方法:grep()

    遇到的问题 今天遇到一个问题,删除数组中的一个指定元素,并返回新的数组. 我定义的js数组是这样的: var sexList=new Array[3]; sexList[0]="1"; sexList[1]="2"; sexList[2]=""; 想达到的效果 我想达到的效果是这样的: 删除索引=1的元素,并返回新数组. 返回的结果是: var sexList=new Array("1",""); 我们知道

  • python 列表删除所有指定元素的方法

    如下所示: a = [1,1,1,2,3,45,1,2,1] a.remove(1) result: [1,1,2,3,45,1,2,1] while 1 in a: a.remove(1) result: [2,3,45,2] 以上这篇python 列表删除所有指定元素的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们. 您可能感兴趣的文章: Python列表删除的三种方法代码分享 Python实现删除列表中满足一定条件的元素示例 Python实现判断并移除列表

  • 查找iframe里元素的方法可传参

    复制代码 代码如下: //查找iframe里面的元素 function FindObject(ss) { var o = null; o = $(window.frames["main"].document).find("#" + ss); return o; }

  • 如何使用XPath提取xml文档数据

    本文实例为大家分享了XPath提取xml文档数据具体代码,供大家参考,具体内容如下 import java.util.List; import org.dom4j.Document; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.junit.Test; /* * 使用XPath查找xml文档数据 * */ public class DemoXPath { @Test //输出book.xml中所有price元素节

  • JS查找数组中重复元素的方法详解

    本文实例讲述了JS查找数组中重复元素的方法.分享给大家供大家参考,具体如下: JS的数据类型有一个数组.今天我们就来谈谈对数组的一种处理.相信很多人都遇到过从数组中查找出不重复的元素,但是我遇到的却是从数组中查找出重复的元素. 从js数组中查找出不重复的元素的方法有很多,下面就给大家列举一个: <!DOCTYPE html> <html> <body> <script> Array.prototype.deleteEle=function(){ var ne

随机推荐