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

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>
              国产精品老牛| 国产精品国产成人国产三级| 国内精品美女av在线播放| 香蕉久久精品日日躁夜夜躁| 亚洲综合色自拍一区| 国产日韩亚洲欧美| 欧美成人高清视频| 欧美日韩免费精品| 性色av一区二区三区| 性色av一区二区三区在线观看| 好看的亚洲午夜视频在线| 欧美成人中文| 欧美视频国产精品| 久久亚洲国产成人| 欧美日韩精品二区| 久久精品亚洲一区二区| 欧美国产亚洲精品久久久8v| 亚洲影视综合| 美女视频黄a大片欧美| 中日韩男男gay无套| 欧美中文字幕视频| 亚洲美女在线看| 亚洲男人的天堂在线aⅴ视频| 一区二区在线观看视频| 亚洲伦理一区| 亚洲成人在线观看视频| 日韩一区二区免费高清| 精品二区久久| 亚洲一区二区少妇| 99伊人成综合| 久久免费国产精品1| 亚洲欧美日韩国产中文在线| 久久午夜羞羞影院免费观看| 亚洲一区在线观看视频 | 一本色道久久综合亚洲精品不 | 一本大道久久a久久精品综合| 亚洲欧美区自拍先锋| 亚洲美女中文字幕| 久久一区二区三区超碰国产精品| 亚洲一区综合| 欧美日韩1区2区| 欧美高清一区二区| 国产一区二区三区久久精品| 一本色道久久88精品综合| 亚洲激情中文1区| 久久久久久久久久看片| 欧美中文字幕精品| 国产精品久久久久一区二区三区 | 一区二区三区视频免费在线观看| 91久久视频| 女主播福利一区| 男女视频一区二区| 国产在线播放一区二区三区| 一本久道久久综合狠狠爱| 亚洲日本va在线观看| 久久男人资源视频| 美女主播视频一区| 韩曰欧美视频免费观看| 欧美永久精品| 久久青草欧美一区二区三区| 国产亚洲精品一区二555| 午夜精品久久久久影视 | 亚洲电影在线看| 久久免费视频这里只有精品| 另类成人小视频在线| 激情五月婷婷综合| 久久综合给合久久狠狠狠97色69| 美女视频一区免费观看| 黄色亚洲免费| 蜜桃av一区二区| 亚洲国产精品一区二区www在线| 一区二区视频欧美| 蜜臀99久久精品久久久久久软件| 欧美激情第六页| 一本一本久久a久久精品牛牛影视| 欧美久久视频| 亚洲午夜精品17c| 欧美中文字幕第一页| 精品成人久久| 欧美激情在线狂野欧美精品| 999在线观看精品免费不卡网站| 亚洲免费影院| 国外成人免费视频| 免费在线观看一区二区| 亚洲精品乱码久久久久久蜜桃麻豆| 日韩视频在线观看国产| 欧美性猛交xxxx免费看久久久| 亚洲主播在线播放| 美女图片一区二区| 中国女人久久久| 国产一区二区三区久久久| 毛片一区二区三区| 一区二区三区欧美视频| 久久偷窥视频| 亚洲图片你懂的| 国模私拍视频一区| 欧美视频在线免费看| 久久国产精品一区二区三区| 欧美成人午夜激情在线| 亚洲欧美综合一区| 亚洲国产日韩一区| 国产精品五月天| 欧美精品乱人伦久久久久久| 亚久久调教视频| 一本久久a久久免费精品不卡| 久久在线播放| 先锋亚洲精品| 亚洲免费观看高清在线观看 | 久久亚洲精品一区| 亚洲天堂av高清| 亚洲精品免费网站| 国产日本欧美一区二区三区在线| 欧美成人一区二区三区在线观看 | 日韩视频在线一区二区三区| 久久午夜影视| 欧美一区二区三区在线看| 日韩网站在线观看| 一区国产精品| 国产性猛交xxxx免费看久久| 欧美日韩中文字幕日韩欧美| 另类天堂av| 久久精彩视频| 亚洲欧美久久久| 在线视频亚洲| 亚洲最新视频在线| 亚洲精品国产品国语在线app| 久久综合色播五月| 欧美影院视频| 亚洲一区在线播放| 亚洲婷婷综合久久一本伊一区| 亚洲精华国产欧美| 亚洲国产精品va在线看黑人动漫| 国产亚洲福利| 国产一区二区高清不卡| 国产女主播一区二区三区| 国产精品丝袜91| 国产伦精品一区二区三区高清版| 欧美午夜视频一区二区| 欧美三级视频在线播放| 欧美日韩另类视频| 欧美日韩网址| 国产精品户外野外| 国产精品大片| 国产女人精品视频| 国产欧美一区二区精品性| 国产欧美一区二区三区在线老狼| 国产老女人精品毛片久久| 国产欧美精品xxxx另类| 国产最新精品精品你懂的| 黄色精品免费| 亚洲激情社区| 夜夜嗨av一区二区三区四季av| 一本一本a久久| 午夜国产一区| 久久久久国产精品麻豆ai换脸| 久久精品国产一区二区电影 | 久久国产精品电影| 久久这里只有精品视频首页| 欧美国产亚洲精品久久久8v| 亚洲国产视频a| 亚洲视频免费看| 久久精品水蜜桃av综合天堂| 久久一综合视频| 欧美日韩国产色视频| 国产精品久久久对白| 国外成人在线| 制服丝袜激情欧洲亚洲| 亚洲男人第一av网站| 久久夜色精品国产欧美乱极品| 亚洲第一天堂无码专区| 在线视频精品一| 久久久久久久久蜜桃| 欧美日韩精品系列| 国产在线播精品第三| 99riav1国产精品视频| 午夜精品www| 欧美黄色免费| 亚洲综合色噜噜狠狠| 米奇777超碰欧美日韩亚洲| 国产精品美女久久久浪潮软件| 国产一区香蕉久久| 中文一区二区| 另类av一区二区| 亚洲伊人网站| 欧美激情精品| 激情成人综合网| 亚洲欧美日韩精品久久久| 免费短视频成人日韩| 亚洲一区亚洲| 欧美日韩国产电影| 亚洲二区在线观看| 久久精品中文字幕免费mv| 亚洲伦理精品| 免费久久99精品国产| 韩国视频理论视频久久| 亚洲欧美日韩系列| 亚洲巨乳在线| 欧美超级免费视 在线| 在线免费观看一区二区三区| 欧美一站二站|