青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

find the nodes in XML in 3 means(XML 二)

Find Specific Elements by Name

Solution

Use the XmlDocument.GetElementsByTagName method, which searches an entire document and returns a System.Xml.XmlNodeList containing any matches

This code demonstrates how you could use GetElementsByTagName to calculate the total price of items in a catalog by retrieving all elements with the name "productPrice":

using System;
using System.Xml;

public class FindNodesByName {

    private static void Main() {

        // Load the document.
        XmlDocument doc = new XmlDocument();
        doc.Load("ProductCatalog.xml");

        // Retrieve all prices.
        XmlNodeList prices = doc.GetElementsByTagName("productPrice");

        decimal totalPrice = 0;
        foreach (XmlNode price in prices) {

            // Get the inner text of each matching element.
            totalPrice += Decimal.Parse(price.ChildNodes[0].Value);
        }

        Console.WriteLine("Total catalog value: " + totalPrice.ToString());
        Console.ReadLine();
    }
}

You can also search portions of an XML document by using the XmlElement.GetElementsByTagName method. It searches all the descendant nodes looking for matches. To use this method, first retrieve an XmlNode that corresponds to an element. Then cast this object to an XmlElement. The following example demonstrates how to find the price node under the first product element.

// Retrieve a reference to the first product.
XmlNode product = doc.GetElementsByTagName("products")[0];

// Find the price under this product.
XmlNode price = ((XmlElement)product).GetElementsByTagName("productPrice")[0];
Console.WriteLine("Price is " + price.InnerText);

If your elements include an attribute of type ID, you can also use a method called GetElementById to retrieve an element that has a matching ID value.

-----------------------------------------------
Get XML Nodes in a Specific XML Namespace

Solution

Use the overload of the XmlDocument.GetElementsByTagName method that requires a namespace name as a string argument. Additionally, supply an asterisk (*) for the element name if you wish to match all tags.

As an example, consider the following compound XML document that includes order and client information, in two different namespaces (http://mycompany/OrderML and http://mycompany/ClientML).

<?xml version="1.0" ?>
<ord:order xmlns:ord="http://mycompany/OrderML"
 xmlns:cli="http://mycompany/ClientML">

  <cli:client>
    <cli:firstName>Sally</cli:firstName>
    <cli:lastName>Sergeyeva</cli:lastName>
  </cli:client>

  <ord:orderItem itemNumber="3211"/>
  <ord:orderItem itemNumber="1155"/>

</ord:order>

Here's a simple console application that selects all the tags in the http://mycompany/OrderML namespace:

using System;
using System.Xml;

public class SelectNodesByNamespace {

    private static void Main() {

        // Load the document.
        XmlDocument doc = new XmlDocument();
        doc.Load("Order.xml");

        // Retrieve all order tags.
        XmlNodeList matches = doc.GetElementsByTagName("*",
          "http://mycompany/OrderML");

        // Display all the information.
        Console.WriteLine("Element \tAttributes");
        Console.WriteLine("******* \t**********");

        foreach (XmlNode node in matches) {

            Console.Write(node.Name + "\t");
            foreach (XmlAttribute attribute in node.Attributes) {
                Console.Write(attribute.Value + "  ");
            }
            Console.WriteLine();
        }
 
        Console.ReadLine();
    }
}

The output of this program is as follows:

Element         Attributes
*******         **********
ord:order       http://mycompany/OrderML  http://mycompany/ClientML
ord:orderItem   3211
ord:orderItem   1155

----------------------------------------------
Find Elements with an XPath Search

For example, consider the following XML document, which represents an order for two items. This document includes text and numeric data, nested elements, and attributes, and so is a good way to test simple XPath expressions.

<?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>

Basic XPath syntax uses a path-like notation. For example, the path /Order/Items/Item indicates an <Item> element that is nested inside an <Items> element, which, in turn, in nested in a root <Order> element. This is an absolute path. The following example uses an XPath absolute path to find the name of every item in an order.

using System;
using System.Xml;

public class XPathSelectNodes {

    private static void Main() {

        // Load the document.
        XmlDocument doc = new XmlDocument();
        doc.Load("orders.xml");

        // Retrieve the name of every item.
        // This could not be accomplished as easily with the
        // GetElementsByTagName() method, because Name elements are
        // used in Item elements and Client elements, and so
        // both types would be returned.
        XmlNodeList nodes = doc.SelectNodes("/Order/Items/Item/Name");
            
        foreach (XmlNode node in nodes) {
            Console.WriteLine(node.InnerText);
        }
     
        Console.ReadLine();
    }
}

The output of this program is as follows:

Electronic Protractor
Invisible Ink

Table 5.1: XPath Expression Syntax

Expression

Description

/

Starts an absolute path that selects from the root node.

/Order/Items/Item selects all Item elements that are children of an Items element, which is itself a child of the root Order element.

//

Starts a relative path that selects nodes anywhere.

//Item/Name selects all the Name elements that are children of an Item element, regardless of where they appear in the document.

@

Selects an attribute of a node.

/Order/@id selects the attribute named id from the root Order element.

*

Selects any element in the path.

/Order/* selects both Items and Client nodes because both are contained by a root Order element.

|

Combines multiple paths.

/Order/Items/Item/Name|Order/Client/Name selects the Name nodes used to describe a Client and the Name nodes used to describe an Item.

.

Indicates the current (default) node.

If the current node is an Order, the expression ./Items refers to the related items for that order.

..

Indicates the parent node.

//Name/.. selects any element that is parent to a Name, which includes the Client and Item elements.

[ ]

Define selection criteria that can test a contained node or attribute value.

/Order[@id="2004-01-30.195496"] selects the Order elements with the indicated attribute value.

/Order/Items/Item[Price > 50] selects products above $50 in price.

/Order/Items/Item[Price > 50 and Name="Laser Printer"] selects products that match two criteria.

starts-with

This function retrieves elements based on what text a contained element starts with.

/Order/Items/Item[starts-with(Name, "C")] finds all Item elements that have a Name element that starts with the letter C.

position

This function retrieves elements based on position.

/Order/Items/Item[position ()=2] selects the second Item element.

count

This function counts elements. You specify the name of the child element to count or an asterisk (*) for all children.

/Order/Items/Item[count(Price) = 1] retrieves Item elements that have exactly one nested Price element.



posted on 2005-11-23 18:13 夢(mèng)在天涯 閱讀(584) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

  • 隨筆 - 461
  • 文章 - 4
  • 評(píng)論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816722
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              99精品热6080yy久久| 亚洲精品影院在线观看| 一区二区三区久久精品| 亚洲国产一区视频| 久久男女视频| 日韩一二三区视频| 一本色道久久综合狠狠躁篇的优点| 欧美日本在线播放| 亚洲欧美在线aaa| 欧美一区在线直播| 亚洲国产成人高清精品| 亚洲欧洲精品一区二区精品久久久| 欧美精品综合| 欧美一区二区在线免费播放| 欧美在线二区| 日韩视频免费| 亚洲欧美一区二区三区极速播放| 狠狠综合久久av一区二区老牛| 亚洲国产精品va| 国产精品久久99| 可以看av的网站久久看| 欧美久久在线| 久久精品国产精品| 欧美激情第10页| 久久精品国产亚洲一区二区三区 | 亚洲精品久久久久久下一站 | 亚洲图片你懂的| 激情亚洲成人| 99国产精品视频免费观看一公开| 国产精品视频网站| 欧美91福利在线观看| 欧美午夜一区| 亚洲国产精品电影在线观看| 国产精品欧美日韩| 亚洲国产欧美一区| 国产婷婷色一区二区三区四区 | 亚洲欧美一区二区三区在线| 久久久天天操| 欧美在线视频网站| 欧美日韩国产高清| 欧美电影在线观看完整版| 欧美性久久久| 亚洲人成毛片在线播放| 激情懂色av一区av二区av| 在线观看不卡| 亚洲无限乱码一二三四麻| 亚洲精品久久久久久久久久久| 欧美一级欧美一级在线播放| 亚洲一区二区三区四区五区午夜| 老**午夜毛片一区二区三区| 久久精品国产2020观看福利| 国产精品国内视频| 亚洲精品一品区二品区三品区| 亚洲国产另类 国产精品国产免费| 亚洲欧美一区二区视频| 亚洲女人天堂av| 欧美色另类天堂2015| 亚洲精品黄色| 99国产精品视频免费观看| 免费日韩成人| 欧美大片免费看| 亚洲成人在线免费| 久久夜色精品一区| 欧美77777| 亚洲激情校园春色| 欧美暴力喷水在线| 亚洲激情视频网站| 99re这里只有精品6| 欧美国产日韩二区| 亚洲精品国产精品国产自| 亚洲精品少妇30p| 欧美日韩岛国| 亚洲一二三级电影| 久久久精品日韩欧美| 国产在线欧美日韩| 久久综合一区二区| 亚洲国产精品尤物yw在线观看| 日韩亚洲国产欧美| 国产精品黄色| 欧美专区在线| 亚洲国产精品久久久久| 99视频一区二区| 国产精品国产亚洲精品看不卡15 | 国产精品日韩欧美大师| 在线一区二区三区四区五区| 亚洲欧美卡通另类91av| 国产美女精品| 久久亚洲私人国产精品va| 欧美激情第六页| 一区二区三区 在线观看视| 国产精品羞羞答答| 久久人人97超碰国产公开结果| 亚洲国产精品女人久久久| 亚洲一区精品视频| 国模套图日韩精品一区二区| 欧美激情一区二区三区在线视频| 一本色道婷婷久久欧美| 久久蜜桃av一区精品变态类天堂| 亚洲黄色片网站| 国产精品亚洲不卡a| 毛片基地黄久久久久久天堂| 亚洲视频大全| 亚洲高清不卡在线| 欧美一二三区在线观看| 亚洲激情第一页| 国产老肥熟一区二区三区| 久热国产精品| 午夜精品一区二区三区在线视| 亚洲承认在线| 久久久国产精彩视频美女艺术照福利| 亚洲免费观看高清在线观看| 国产视频在线观看一区二区| 欧美日韩第一区| 裸体一区二区| 欧美一激情一区二区三区| 亚洲精品一区二区三区99| 久久一区激情| 性欧美精品高清| 一区二区久久久久| 亚洲国产婷婷| 在线观看成人网| 国产欧美一区二区精品性色| 欧美日韩视频一区二区三区| 欧美亚洲一区在线| 亚洲精品综合精品自拍| 国产永久精品大片wwwapp| 欧美性大战久久久久久久蜜臀| 欧美成人国产一区二区| 久久美女性网| 久久精品99| 性做久久久久久免费观看欧美| 一本色道久久综合亚洲精品不卡 | 亚洲欧美卡通另类91av| 亚洲精选在线| 亚洲欧洲一区二区在线播放| 欧美韩国日本综合| 免费久久久一本精品久久区| 久久综合狠狠| 久久综合久色欧美综合狠狠| 久久精品一区蜜桃臀影院| 午夜精品成人在线| 午夜精品久久久久久| 亚洲综合国产| 欧美亚洲系列| 久久久久国产精品厨房| 欧美综合国产精品久久丁香| 欧美在线视频一区| 久久国产精品黑丝| 久久久久久久久久久久久9999| 久久九九国产| 欧美ab在线视频| 亚洲福利免费| 日韩亚洲欧美在线观看| 一区二区三区精密机械公司| 一区二区三区欧美亚洲| 亚洲女人天堂av| 欧美一区二区三区在线观看视频| 欧美在线地址| 免费观看亚洲视频大全| 欧美日韩第一页| 国产精品实拍| 在线成人中文字幕| 亚洲乱码国产乱码精品精| 一区二区三区波多野结衣在线观看| 制服诱惑一区二区| 久久福利影视| 亚洲国产成人久久| 亚洲精品国产精品国产自| 一区二区三区精品在线| 欧美一区二区三区免费在线看| 久久一区二区三区四区五区| 欧美精品在线网站| 国产日韩欧美精品一区| 亚洲国产激情| 亚洲一区中文| 久久这里有精品视频| 亚洲高清视频一区二区| 亚洲午夜成aⅴ人片| 久久激情一区| 欧美午夜视频网站| 伊人久久大香线蕉av超碰演员| 一区二区三区 在线观看视频| 久久av二区| 亚洲另类春色国产| 久久国产精品久久国产精品| 欧美日韩播放| 国产综合av| 亚洲欧美视频| 亚洲欧洲一二三| 久久九九久久九九| 国产精品黄视频| 日韩小视频在线观看专区| 久久精品噜噜噜成人av农村| 亚洲美女视频在线观看| 久久伊人亚洲| 国产一区二区三区四区在线观看| 一本色道久久综合亚洲91| 国产精品久久久一区二区三区| 亚洲日本无吗高清不卡| 蜜桃久久精品乱码一区二区|