原文 http://www.csharp-examples.net/xml-nodes-by-name/

To find nodes in an XML file you can use XPath expressions. Method XmlNode.Selec­tNodes returns a list of nodes selected by the XPath string. Method XmlNode.Selec­tSingleNode finds the first node that matches the XPath string.

Suppose we have this XML file.

[XML]

<Names>
<Name>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</Name>
<Name>
<FirstName>James</FirstName>
<LastName>White</LastName>
</Name>
</Names>

To get all <Name> nodes use XPath expression /Names/Name. The first slash means that the <Names> node must be a root node. SelectNodes method returns collection XmlNodeList which will contain the <Name> nodes. To get value of sub node <FirstName> you can simply index XmlNode with the node name: xmlNode["FirstName"].InnerText. See the example below.

[C#]

XmlDocument xml = new XmlDocument();
xml.LoadXml(myXmlString); // suppose that myXmlString contains "<Names>...</Names>" XmlNodeList xnList = xml.SelectNodes("/Names/Name");
foreach (XmlNode xn in xnList)
{
string firstName = xn["FirstName"].InnerText;
string lastName = xn["LastName"].InnerText;
Console.WriteLine("Name: {0} {1}", firstName, lastName);
}

The output is:

Name: John Smith
Name: James White

Select XML Nodes by Name [C#]的更多相关文章

  1. Using TXMLDocument, Working with XML Nodes

    Using TXMLDocument The starting point for working with an XML document is the Xml.XMLDoc.TXMLDocumen ...

  2. C#中读取xml文件指定节点

    目录(?)[-] XmlDocumentSelectSingleNode方法的使用 XmlDocumentSelectNodes方法的使用 通过节点属性查找指定节点   参考:Select XML N ...

  3. 转载---SQL Server XML基础学习之<6>--XQuery的 value() 方法、 exist() 方法 和 nodes() 方法

    /*------------------------------------------------------------------------------+ #| = : = : = : = : ...

  4. xml类型使用注意事项

    xml 的数据类型在平常的开发也是很常用的,燃鹅.也是有一些地方需要留意.今天我就分享几个测试的例子. 使用 xquery.exist (有但不仅仅限于)的注意事项.通常使用来判断节点是否存在,值是否 ...

  5. LINQ系列:LINQ to XML查询

    1. 读取XML文件 XDocument和XElement类都提供了导入XML文件的Load()方法,可以读取XML文件的内容,并转换为XDocument或XElement类的实例. 示例XML文件: ...

  6. XQuery的 value() 方法、 exist() 方法 和 nodes() 方法

    Xml数据类型 /*------------------------------------------------------------------------------+ #| = : = : ...

  7. 【译】用jQuery 处理XML-- jQuery与XML

    用jQuery 处理XML--写在前面的话 用jQuery 处理XML-- DOM(文本对象模型)简介 用jQuery 处理XML--浏览器中的XML与JavaScript 用jQuery 处理XML ...

  8. 在SQL2008中使用XML应对不确定结构的参数

    目的:统一接口,当数据结构发生变化时,前后端业务接口不发生变化,由业务具体解析结构. 规则:确定的接口用参数表(多行提交),不确定的参数用XML   DECLARE @r TABLE     (    ...

  9. 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参

    转载至: http://www.cnblogs.com/lzrabbit/archive/2012/04/29/2475427.html 在上一篇Sql Server参数化查询之where in和li ...

随机推荐

  1. poj1323--贪心算法

    题意:一群人打牌包括你,每人出一张牌,谁最大,谁就算赢一局,问你最少能赢几局? 给出人数N,每人的牌数M,及你的牌. 分析:1.这题需比较大小,就像我们打牌时要将牌排序以便出牌,显然要先将手上的牌进行 ...

  2. OC语法4——自定义构造方法,description方法

    自定义构造方法: 我们已经知道创建对象分两步,1:在内存中开辟存储空间,并把地址存储在指针变量里,2:调用指针变量的初始化方法init初始化该对象. Student * stu = [Student ...

  3. 持续集成 之 apache-continuum

    作者:许振坪,http://blog.csdn.net/benkaoya 1.前言 最近在研究持续集成,摸索了很多持续集成的工具,Apache Continuum也包括其中.既然飞过,那就留下点什么吧 ...

  4. 转:Server.MapPath相关

    如果你从Page类继承的类中执行这条语句,才可以简单地使用 DataBase = Server.MapPath("data.mdb");否则写全命名空间:System.Web.Ht ...

  5. GoF——抽象工厂模式

    抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类.

  6. mini-httpd源码分析-mini-httpd.c之外总结

    version.h #define SERVER_SOFTWARE "mini_httpd/1.21 18oct2014" #define SERVER_URL "htt ...

  7. SendMessage基本认识

    SendMessage基本认识 函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.而函数PostMessage不同,将一个消息寄送到一个线 ...

  8. iOS开发之主题皮肤

    iOS开发之主题皮肤 分类: [iOS]  最近在开发一款[公交应用],里面有个模块涉及到主题设置,这篇文章主要谈一下个人的做法. 大概的步骤如下: (1):整个应用依赖于一个主题管理器,主题管理器根 ...

  9. poj2521---lose money

    #include <stdio.h> #include <stdlib.h> int rever(int n) { return -n; } int main() { int ...

  10. 城市平乱(Bellman)

    城市平乱 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市 ...