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

C++ Programmer's Cookbook

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

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1818927
  • 排名 - 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一区| 亚洲二区视频| 久久久久久久成人| 亚洲成人影音| 亚洲福利视频专区| 欧美精品99| 中国女人久久久| 亚洲私人影院| 国产九色精品成人porny| 久久国产精品99国产精| 欧美一区二区视频观看视频| 激情一区二区三区| 亚洲欧洲三级电影| 欧美性片在线观看| 久久精品女人| 美女精品一区| 亚洲一区二区成人| 欧美在线影院| 99re亚洲国产精品| 亚洲主播在线| 亚洲国产精品一区二区第四页av | 美女爽到呻吟久久久久| 久久综合色8888| 99热这里只有精品8| 一区二区三区四区五区视频| 国产日韩在线不卡| 亚洲激情视频在线播放| 国产农村妇女精品一区二区| 免费短视频成人日韩| 国产精品电影观看| 欧美va天堂在线| 国产精品一区二区女厕厕| 欧美 日韩 国产在线| 欧美午夜久久久| 欧美jizz19性欧美| 国产精品一区久久| 亚洲精品视频在线| 亚洲国产成人久久| 亚洲欧美日韩第一区| 亚洲毛片视频| 久久频这里精品99香蕉| 亚洲小说春色综合另类电影| 久久久蜜桃一区二区人| 小黄鸭精品aⅴ导航网站入口| 免费在线播放第一区高清av| 欧美资源在线观看| 欧美日韩在线视频一区二区| 欧美高清视频一区| 国语精品一区| 午夜精品视频在线观看一区二区| 日韩视频永久免费观看| 久久久亚洲午夜电影| 久久精品国产一区二区电影| 国产精品v欧美精品v日本精品动漫| 女同性一区二区三区人了人一| 国产精品揄拍一区二区| 亚洲午夜精品网| 亚洲夜晚福利在线观看| 欧美精品在线一区二区三区| 欧美高清成人| 亚洲国产精品久久久久婷婷884| 性久久久久久| 欧美一区二区在线播放| 国产精品美女久久久浪潮软件| 亚洲精品免费一二三区| 日韩视频免费| 欧美精品在线极品| 99ri日韩精品视频| 亚洲调教视频在线观看| 欧美三级黄美女| 一区二区三区毛片| 亚洲欧美偷拍卡通变态| 国产欧美精品一区aⅴ影院| 午夜精品久久久久久久男人的天堂 | 欧美日韩一区二区国产| 亚洲人成亚洲人成在线观看| 日韩一级在线观看| 欧美日韩精品一区二区在线播放 | 欧美亚洲免费电影| 国产乱人伦精品一区二区| 亚洲欧美日韩国产成人精品影院| 欧美在线观看www| 国产午夜精品全部视频播放| 久久精品免费电影| 欧美国产综合一区二区| 一区二区高清| 国产精品综合久久久| 久久激情综合网| 亚洲国产欧美在线| 亚洲欧美日韩直播| 狠狠色丁香婷婷综合| 欧美华人在线视频| 这里只有精品视频在线| 久久久久九九九| 日韩亚洲精品在线| 国产伦精品一区二区三区免费| 久久久www成人免费无遮挡大片 | 久久欧美中文字幕| 亚洲人成在线免费观看| 欧美亚洲第一区| 久久久久久穴| 一区二区高清| 久久久亚洲人| 亚洲永久免费精品| 亚洲大片免费看| 国产精品美女午夜av| 久久阴道视频| 亚洲一二三级电影| 亚洲高清视频一区二区| 性欧美18~19sex高清播放| 亚洲国产成人高清精品| 国产精品美女视频网站| 欧美大片免费久久精品三p| 亚洲欧美国产另类| 亚洲精品在线免费| 麻豆精品一区二区综合av | 狠狠色丁香婷婷综合| 欧美日韩亚洲国产一区| 久久影院午夜论| 亚洲一区二区日本| 亚洲人妖在线| 欧美xart系列高清| 久久精品2019中文字幕| 亚洲午夜电影| 一本色道久久综合亚洲91 | 欧美日韩精品一区二区天天拍小说 | 久久久久久一区二区三区| 亚洲永久免费精品| 99日韩精品| 亚洲国产欧美一区| 欧美国产免费| 久久久噜久噜久久综合| 欧美在线精品一区| 午夜亚洲影视| 亚洲小视频在线观看| 99精品国产在热久久下载| 亚洲国产精品嫩草影院| 有码中文亚洲精品| 激情久久久久久久| 精品99一区二区| 国产手机视频精品| 国产欧美一区二区三区另类精品| 国产精品成人播放| 欧美日韩直播| 欧美日韩一区二区三| 欧美三区美女| 国产精品国产福利国产秒拍| 欧美日韩综合在线| 国产精品国产三级国产普通话蜜臀| 欧美日韩精品久久| 国产精品豆花视频| 国产精品香蕉在线观看| 国产精品视频网址| 国产手机视频精品| 黄色免费成人| 亚洲黄色在线看| 91久久精品一区| 99这里只有久久精品视频| 亚洲新中文字幕| 久久国产精品色婷婷| 久久香蕉国产线看观看网| 美女露胸一区二区三区| 亚洲国产天堂久久国产91| 99精品福利视频| 亚洲欧洲av一区二区| 久久久久久一区| 欧美好骚综合网| 国产精品久久久久毛片大屁完整版| 国产精品久久久久秋霞鲁丝| 国产情人节一区| 亚洲国产另类久久久精品极度| 亚洲日本aⅴ片在线观看香蕉| 国产精品99久久99久久久二8| 欧美一区二区精品在线| 麻豆精品传媒视频| 一本色道久久综合狠狠躁篇的优点 | 欧美~级网站不卡| 国产精品www.| 国内精品久久久久久久影视蜜臀| 有坂深雪在线一区| 亚洲线精品一区二区三区八戒| 久久久999精品| 亚洲精品一区在线观看香蕉| 亚洲欧美日韩在线| 免费视频久久| 国产乱理伦片在线观看夜一区| 在线成人av网站| 亚洲欧美日韩天堂| 欧美成人综合网站| 亚洲欧美精品| 欧美国产日韩精品免费观看| 国产欧美日韩一区| 日韩视频免费大全中文字幕| 久久久成人网| 99精品黄色片免费大全| 久久久久久有精品国产| 国产精品女主播| 99综合电影在线视频| 欧美电影免费观看高清|