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

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 夢在天涯 閱讀(586) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1818973
  • 排名 - 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>
              两个人的视频www国产精品| 免费亚洲一区二区| 99re亚洲国产精品| 欧美区二区三区| 一区二区三区久久| 亚洲最快最全在线视频| 国产精品hd| 午夜亚洲影视| 午夜久久tv| 亚洲电影在线| 亚洲精品1区2区| 欧美日韩不卡一区| 午夜精品久久久久久久99水蜜桃| 亚洲欧美日韩另类| 激情五月综合色婷婷一区二区| 免费永久网站黄欧美| 欧美激情麻豆| 欧美伊人久久久久久久久影院| 久久久999| 亚洲视频狠狠| 久久国产88| 日韩午夜av电影| 午夜在线观看欧美| 一区二区高清在线观看| 欧美亚洲在线| av成人手机在线| 欧美在线电影| 亚洲自拍偷拍麻豆| 免费av成人在线| 久久成人精品视频| 欧美日韩亚洲三区| 美女国产一区| 国产精品久久一区主播| 欧美电影免费| 国产亚洲福利社区一区| 亚洲精品国产日韩| 国色天香一区二区| 亚洲免费观看| 亚洲黄色一区二区三区| 香蕉久久a毛片| 亚洲午夜精品网| 欧美第十八页| 欧美高清一区| 一区二区三区在线观看视频| 亚洲一区二区3| 亚洲日本一区二区| 久久久久久久网站| 欧美在线观看视频在线| 欧美日韩国产综合视频在线| 欧美高清免费| 黄色精品一区| 欧美在线视屏| 久久久xxx| 国产伦精品一区| 一区二区三区精品久久久| 99xxxx成人网| 欧美激情综合在线| 亚洲国产精品视频| 亚洲欧洲一区二区天堂久久| 久久午夜羞羞影院免费观看| 久久久久中文| 国内在线观看一区二区三区| 亚洲欧美一级二级三级| 小辣椒精品导航| 国产精品入口66mio| 亚洲一区三区视频在线观看| 亚洲主播在线播放| 国产精品国产福利国产秒拍| 一区二区激情| 欧美亚洲视频在线观看| 国产精品一级二级三级| 亚洲欧美日韩国产精品| 久久精品国产成人| 国产伦精品一区二区三| 欧美一区二区三区在| 久久久久国产一区二区三区| 极品尤物av久久免费看| 麻豆成人综合网| 亚洲韩国日本中文字幕| 日韩一区二区高清| 国产精品久久久久久久app| 亚洲一区二区三区中文字幕| 久久国产精品一区二区三区四区| 国产一区二区精品久久99| 久久在线91| 亚洲国产裸拍裸体视频在线观看乱了| 日韩亚洲精品视频| 国产精品久久久一区二区| 午夜欧美视频| 欧美激情区在线播放| 在线亚洲高清视频| 国产精品网站在线观看| 久久全球大尺度高清视频| 欧美成va人片在线观看| 在线视频精品一区| 欧美小视频在线| 久久高清一区| 99riav久久精品riav| 久久国产福利国产秒拍| 亚洲激情图片小说视频| 国产精品一区久久久| 看片网站欧美日韩| 在线视频精品一区| 蜜臀av在线播放一区二区三区| 亚洲天堂免费在线观看视频| 狠狠色丁香久久综合频道| 欧美日韩精品免费| 久久久久久久一区二区| 在线亚洲高清视频| 欧美国产日韩xxxxx| 午夜精品福利一区二区蜜股av| 1024亚洲| 国产欧美日韩三级| 欧美日韩精品在线观看| 蜜臀99久久精品久久久久久软件| 一区二区三区欧美亚洲| 欧美国产一区二区在线观看| 欧美一区二区| 在线综合+亚洲+欧美中文字幕| 影音先锋久久精品| 国产欧美精品一区二区三区介绍| 欧美成人免费全部| 久久精品亚洲乱码伦伦中文| 亚洲免费综合| 一本色道久久88综合日韩精品| 欧美不卡高清| 久久深夜福利免费观看| 性做久久久久久免费观看欧美| 99v久久综合狠狠综合久久| 亚洲高清毛片| 精品成人久久| 精品动漫3d一区二区三区| 国产视频不卡| 国产欧美丝祙| 国产乱码精品一区二区三区忘忧草| 欧美日韩在线视频观看| 欧美区一区二| 欧美精品在线视频观看| 欧美成人一区二区在线| 免费人成网站在线观看欧美高清 | 一级日韩一区在线观看| 在线国产精品播放| 精品福利免费观看| 亚洲第一中文字幕| 亚洲国内欧美| 亚洲精品一级| 正在播放亚洲| 亚洲女爱视频在线| 欧美一区国产一区| 久久久久综合网| 麻豆视频一区二区| 免费在线视频一区| 亚洲高清影视| 日韩网站在线看片你懂的| 99热这里只有成人精品国产| 一区二区不卡在线视频 午夜欧美不卡'| 亚洲精品一二区| 在线亚洲国产精品网站| 亚洲欧美综合国产精品一区| 午夜亚洲福利在线老司机| 久久久久久久久久久久久女国产乱 | 女主播福利一区| 最新亚洲电影| 亚洲夜间福利| 久久九九精品99国产精品| 免费在线观看一区二区| 欧美理论电影网| 国产精品v欧美精品v日韩精品| 国产精品一区视频网站| 在线不卡a资源高清| 亚洲精品免费一二三区| 亚洲欧美日韩专区| 久久另类ts人妖一区二区 | 午夜精品99久久免费| 久久久久久夜精品精品免费| 欧美黄色视屏| 亚洲一二区在线| 免费欧美日韩| 国产欧美短视频| 亚洲美女毛片| 欧美专区在线观看| 亚洲国产91精品在线观看| 亚洲欧美日韩精品久久| 欧美成年人网站| 国产专区欧美专区| 亚洲视频网在线直播| 久久综合中文| 亚洲网站在线| 欧美11—12娇小xxxx| 国产一区二区三区高清在线观看| 日韩视频一区| 欧美+日本+国产+在线a∨观看| 一区二区三区波多野结衣在线观看| 久久国产视频网| 国产精品毛片| 一区二区三区四区五区精品| 欧美成人精精品一区二区频| 亚洲欧洲av一区二区三区久久| 欧美电影电视剧在线观看| 黄色成人av在线|