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

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

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 夢在天涯 閱讀(580) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811733
  • 排名 - 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在线免费观看| 国产综合久久| 亚洲娇小video精品| 欧美手机在线视频| 久久久av网站| 欧美精品 日韩| 欧美一区二区三区喷汁尤物| 久久精品91久久久久久再现| 亚洲人成网在线播放| 亚洲小说欧美另类婷婷| 精东粉嫩av免费一区二区三区| 亚洲高清久久网| 国产精品视频xxxx| 亚洲电影观看| 国产伦精品一区二区三区四区免费| 玖玖国产精品视频| 国产精品va| 欧美国产日韩亚洲一区| 国产免费成人| 亚洲精品一区二区三区樱花| 好看的日韩视频| 一二三区精品福利视频| 亚洲国产欧美一区| 亚洲欧美制服另类日韩| 99re热精品| 久久男人av资源网站| 亚洲在线网站| 欧美福利在线| 美女视频黄免费的久久| 国产欧美视频在线观看| 亚洲乱码视频| 亚洲黄色有码视频| 久久精品一本| 久久久精品一区| 国产精品普通话对白| 日韩视频在线免费观看| 亚洲激情影院| 另类激情亚洲| 蜜臀99久久精品久久久久久软件| 国产精品你懂的在线| aⅴ色国产欧美| 99re66热这里只有精品3直播| 久久一区亚洲| 美女久久网站| 这里只有精品电影| 99伊人成综合| 欧美日韩激情小视频| 亚洲精选在线| 亚洲视频久久| 国产精品久久午夜| 一本色道婷婷久久欧美| 亚洲一区网站| 国产精品欧美经典| 欧美一级网站| 猫咪成人在线观看| 亚洲黑丝在线| 欧美日本国产在线| a4yy欧美一区二区三区| 亚洲欧美一区二区激情| 国产精品美女主播在线观看纯欲| 亚洲一区二区三区在线播放| 欧美在线免费| 一区二区三区在线视频观看| 老司机精品福利视频| 欧美福利视频在线观看| 99re热这里只有精品视频| 欧美午夜精品一区二区三区| 亚洲一区美女视频在线观看免费| 欧美一区二区三区电影在线观看| 国产日韩欧美成人| 久久久女女女女999久久| 免费欧美在线视频| 夜夜嗨av一区二区三区中文字幕| 欧美日韩伦理在线免费| 亚洲女与黑人做爰| 久久中文久久字幕| 9i看片成人免费高清| 国产精品视频一二三| 久久精品视频一| 欧美激情一区二区三区全黄| 亚洲视频免费看| 国产日韩欧美综合一区| 蜜桃av噜噜一区| 日韩一级精品视频在线观看| 欧美一区二区三区免费视| 在线观看三级视频欧美| 欧美日韩在线观看视频| 羞羞视频在线观看欧美| 欧美成人精品三级在线观看| 亚洲视频播放| 国外成人网址| 国产精品成人免费| 久久五月天婷婷| 夜夜嗨av色综合久久久综合网| 久久久xxx| 99精品视频免费观看| 国产一区二区在线免费观看| 欧美精品一区二区三区久久久竹菊| 亚洲免费视频成人| 亚洲国内高清视频| 久久久久一区二区三区| 亚洲一区区二区| 91久久国产精品91久久性色| 国产欧美一区二区三区在线老狼 | 篠田优中文在线播放第一区| 亚洲国产美女精品久久久久∴| 国产精品久久国产三级国电话系列| 久久精品道一区二区三区| 99成人在线| 亚洲国产精品精华液2区45| 欧美一区二区三区免费视| aa成人免费视频| 在线国产亚洲欧美| 国产色婷婷国产综合在线理论片a| 欧美日韩在线观看一区二区| 欧美freesex交免费视频| 欧美一区二区精品在线| 这里只有精品在线播放| 日韩视频精品在线| 亚洲电影av在线| 欧美国产综合| 欧美岛国在线观看| 免费黄网站欧美| 卡通动漫国产精品| 久久久久久久久久久久久9999| 午夜一区二区三视频在线观看| 一区二区三区欧美日韩| 99riav国产精品| 亚洲精品国产品国语在线app| 亚洲高清在线| 亚洲黄色小视频| 亚洲日本aⅴ片在线观看香蕉| 亚洲国产精品久久久久秋霞影院 | 欧美激情综合色综合啪啪| 免费日韩视频| 欧美高清视频www夜色资源网| 美女啪啪无遮挡免费久久网站| 久久久午夜电影| 麻豆av一区二区三区| 欧美gay视频| 欧美日韩在线免费观看| 国产精品久久久久影院色老大| 国产精品电影观看| 国产精品青草久久久久福利99| 国产视频不卡| 伊人色综合久久天天| 亚洲国产一区视频| 99成人在线| 性色av一区二区三区| 久久天天躁狠狠躁夜夜av| 欧美成人一区二免费视频软件| 亚洲成色www8888| 日韩视频在线你懂得| 午夜精品福利一区二区蜜股av| 欧美一区二区高清| 欧美3dxxxxhd| 国产精品久久久久影院色老大 | 欧美三级电影精品| 国产日韩精品视频一区二区三区| 国内精品久久久久久影视8| 亚洲人成人99网站| 亚洲欧美国产一区二区三区| 久久综合给合久久狠狠色| 亚洲激情网站| 亚洲欧美国产制服动漫| 蘑菇福利视频一区播放| 国产精品国产一区二区| 亚洲成色777777在线观看影院 | 国产精品免费观看在线| 在线看片欧美| 亚洲欧美经典视频| 嫩草成人www欧美| 亚洲一区二区成人在线观看| 噜噜噜在线观看免费视频日韩| 欧美日韩专区| 亚洲黄色有码视频| 久久精品91久久香蕉加勒比| 亚洲精品欧美| 亚洲欧美日韩综合| 欧美日韩国产在线播放网站| 国内精品久久久久影院薰衣草| 亚洲午夜日本在线观看| 欧美福利电影在线观看| 欧美中文在线免费| 国产精品美女久久久久久2018| 亚洲破处大片| 蜜臀91精品一区二区三区|