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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設計模式} {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 夢在天涯 閱讀(584) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統(tǒng)計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816722
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              久热综合在线亚洲精品| 欧美日韩伊人| 一区二区在线视频播放| 久久国产精品久久久久久久久久 | 91久久综合亚洲鲁鲁五月天| 久久午夜精品一区二区| 久久亚洲一区| 亚洲免费观看高清完整版在线观看| 欧美激情精品久久久六区热门 | 欧美成人精品影院| 一区二区日韩| 亚洲天堂av电影| 国产资源精品在线观看| 欧美成人一区二免费视频软件| 欧美电影在线免费观看网站| 一区二区三区免费网站| 亚洲一级在线观看| 精东粉嫩av免费一区二区三区| 欧美福利在线| 国产精品激情偷乱一区二区∴| 久久激情一区| 欧美精品一区三区在线观看| 午夜精品在线| 美女图片一区二区| 亚洲永久网站| 久久综合久久综合这里只有精品 | 1000部精品久久久久久久久| 亚洲国产日韩美| 国产精品成人免费精品自在线观看| 欧美一级免费视频| 久久综合伊人| 午夜国产精品影院在线观看| 久久综合久久久| 午夜亚洲伦理| 欧美成人首页| 久久久久久久久久久久久女国产乱| 免费精品视频| 久久精品免费播放| 欧美日韩一级大片网址| 美女诱惑一区| 国产欧美视频一区二区三区| 亚洲国产精品欧美一二99| 国产欧美日本| 在线视频精品一区| 亚洲精品老司机| 久久福利影视| 欧美一区二区观看视频| 欧美国产一区二区| 美女在线一区二区| 国产色视频一区| 一区二区三区三区在线| 亚洲伦理在线免费看| 久久久久久国产精品mv| 欧美一区二区三区四区在线| 欧美日韩妖精视频| 最近中文字幕日韩精品| 亚洲国产日韩欧美在线99| 欧美一区二区视频在线观看2020| 亚洲欧美日韩一区| 欧美午夜免费影院| 99在线精品视频| 亚洲视频电影在线| 欧美日韩在线一区二区| 亚洲精品欧洲| 一本色道久久综合亚洲精品小说| 你懂的国产精品永久在线| 蜜臀av性久久久久蜜臀aⅴ四虎| 国产欧美午夜| 欧美主播一区二区三区美女 久久精品人 | 欧美日本不卡| 亚洲激情综合| 亚洲视频在线观看网站| 欧美日韩激情网| 一本大道久久精品懂色aⅴ| 一区二区三区成人精品| 欧美日韩国产欧美日美国产精品| 亚洲欧洲久久| 亚洲午夜电影网| 国产精品国产三级国产普通话99 | 欧美激情精品久久久久久大尺度| 在线成人激情黄色| 欧美aⅴ99久久黑人专区| 亚洲国产专区校园欧美| 一区二区三区久久| 国产精品日韩久久久久| 亚欧美中日韩视频| 久久综合色88| 亚洲精品网站在线播放gif| 欧美日韩精品一区二区三区四区| 99国产精品视频免费观看| 午夜精彩国产免费不卡不顿大片| 国产一区二区三区在线观看免费视频 | 久久久久久综合| 亚洲第一视频网站| 在线视频你懂得一区| 国产精品欧美日韩一区| 久久久蜜桃一区二区人| 最新高清无码专区| 欧美亚洲在线播放| 亚洲黄色免费电影| 国产精品免费一区二区三区观看 | 欧美国产日韩精品免费观看| 亚洲校园激情| 激情综合网激情| 欧美日韩另类在线| 久久精品视频播放| aⅴ色国产欧美| 免费的成人av| 亚洲欧美亚洲| 亚洲人成在线观看一区二区 | 久久精品一二三| 日韩午夜中文字幕| 噜噜噜躁狠狠躁狠狠精品视频| 在线一区观看| 樱桃成人精品视频在线播放| 欧美午夜免费影院| 欧美成人精品在线观看| 欧美一区二区久久久| 99这里只有精品| 欧美激情精品久久久久久| 欧美一区影院| 亚洲一区网站| 亚洲日本免费| 影音先锋中文字幕一区| 国产美女诱惑一区二区| 欧美日韩成人一区二区三区| 久久午夜国产精品| 欧美一区免费视频| 亚洲小说春色综合另类电影| 亚洲精品一区二区三区蜜桃久| 毛片一区二区三区| 久久久999| 欧美亚洲免费| 午夜精品久久久久久久99樱桃| 亚洲精品免费看| 亚洲二区精品| 狠狠久久婷婷| 国产综合色在线| 国产亚洲精品久久久久动| 国产精品卡一卡二卡三| 欧美日韩三区| 欧美日韩免费一区二区三区| 欧美激情亚洲一区| 欧美精品一区二区三区蜜桃| 毛片基地黄久久久久久天堂| 久久综合影视| 久久综合伊人77777| 久久综合导航| 麻豆精品91| 欧美大学生性色视频| 欧美福利一区二区三区| 欧美成年人视频| 欧美激情免费观看| 欧美日韩在线精品一区二区三区| 欧美日本高清| 国产精品久久久久999| 国产精品第一页第二页第三页| 国产精品高清网站| 国产欧美精品一区二区三区介绍| 国产毛片久久| 精品999成人| 亚洲国产中文字幕在线观看| 日韩一级不卡| 亚洲欧美久久久| 久久国产夜色精品鲁鲁99| 久久综合图片| 亚洲电影第1页| 9l国产精品久久久久麻豆| 亚洲欧美激情视频| 久久久久久伊人| 欧美激情视频在线免费观看 欧美视频免费一 | 亚洲国产cao| 亚洲视频精选| 久久精品网址| 欧美日韩国语| 国产视频自拍一区| 亚洲激情成人| 午夜精品一区二区三区在线播放| 久久亚洲综合| 一区二区三区免费在线观看| 欧美一区二区免费| 欧美激情一区二区三级高清视频| 国产精品美女久久久免费| 在线免费一区三区| 亚洲制服丝袜在线| 欧美高清hd18日本| 亚洲一区三区视频在线观看| 久久婷婷影院| 国产精品嫩草影院av蜜臀| 亚洲高清视频的网址| 午夜精品在线| 亚洲清纯自拍| 久久久999成人| 国产精品久久久久久久久免费樱桃| 红桃视频国产精品| 欧美一级视频一区二区| 亚洲人体影院| 裸体一区二区三区| 国产伪娘ts一区| 亚洲欧美999|