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

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>
              在线亚洲高清视频| 国产精品福利在线| 久久精品亚洲一区二区| 欧美色欧美亚洲另类七区| 欧美韩日一区二区| 影音先锋另类| 久久成人综合视频| 久久久精彩视频| 国产一区二区三区在线观看免费视频| 日韩一区二区精品视频| 日韩亚洲精品视频| 欧美理论在线| 日韩小视频在线观看| 99视频一区二区三区| 欧美电影免费观看大全| 亚洲国产导航| 亚洲精品国产系列| 欧美精品福利在线| 99re热精品| 亚洲欧美日韩一区二区在线 | 久久精品国产77777蜜臀| 久久精品国产在热久久| 国产欧美综合一区二区三区| 亚洲欧美影院| 久久久国产精品一区| 狠狠久久五月精品中文字幕| 久久精品在线视频| 欧美成人精品一区| 91久久午夜| 欧美日韩日韩| 亚洲你懂的在线视频| 久久精品一区中文字幕| 黄色成人免费观看| 欧美成人69| 亚洲免费电影在线观看| 午夜精品久久久久久久99水蜜桃| 国产精品一区二区你懂的| 欧美在线视频观看| 欧美激情精品久久久久久黑人| 亚洲精品欧洲| 国产精品少妇自拍| 久久久综合精品| 日韩视频在线观看| 久久精品2019中文字幕| 亚洲欧洲午夜| 国产精品免费小视频| 久久精品视频免费| 亚洲精品久久久久久久久久久久 | 亚洲在线视频一区| 玖玖在线精品| 亚洲视频一二| 很黄很黄激情成人| 欧美日韩中字| 久久中文久久字幕| 中文精品一区二区三区| 免费成人高清在线视频| 亚洲图片在线观看| 在线成人av| 国产精品女主播在线观看| 久久综合九色综合网站| 中文一区二区在线观看| 欧美成人一区二区三区| 亚洲综合视频1区| 亚洲大胆人体视频| 国产精品自拍视频| 欧美激情精品久久久久久大尺度 | 亚洲一区二区影院| 亚洲电影在线免费观看| 久久高清国产| 亚洲午夜在线观看| 亚洲日韩成人| 黄色成人在线免费| 国产精品剧情在线亚洲| 欧美韩日视频| 久久综合伊人77777蜜臀| 亚洲在线观看免费视频| 亚洲精品三级| 亚洲丰满在线| 免费不卡在线视频| 久久视频这里只有精品| 欧美亚洲一级片| 亚洲天堂av图片| 亚洲另类视频| 亚洲国产色一区| 在线不卡亚洲| 激情综合亚洲| 国产一区二区丝袜高跟鞋图片| 欧美视频中文一区二区三区在线观看| 米奇777超碰欧美日韩亚洲| 久久精品一区二区三区不卡| 亚洲免费在线电影| 亚洲一区二区三区精品动漫| av成人老司机| 一区二区三区黄色| 中文在线不卡视频| 中日韩午夜理伦电影免费| aa级大片欧美三级| 一区二区三区高清在线| 一级成人国产| 亚洲一区二区在线视频| 亚洲一区二区三区影院| 亚洲一区二区视频在线| 亚洲综合视频一区| 午夜欧美精品久久久久久久| 午夜欧美精品| 久久久xxx| 久热精品视频| 欧美高清视频免费观看| 欧美激情导航| 欧美日韩在线精品| 国产精品免费一区二区三区在线观看 | 国产一区二区成人久久免费影院| 国产精品羞羞答答xxdd| 国产精品一区二区男女羞羞无遮挡| 国产精品久久久久久亚洲调教 | 国产一区视频网站| 亚洲国产欧美一区二区三区同亚洲| 精品电影一区| 亚洲精品视频在线观看网站| 中文欧美字幕免费| 欧美在线观看网址综合| 久久影院亚洲| 亚洲欧洲在线一区| 亚洲一区二区欧美| 久久久噜噜噜久噜久久| 欧美高清在线精品一区| 欧美吻胸吃奶大尺度电影| 国产欧美日韩高清| 亚洲国产欧美精品| 亚洲欧美精品伊人久久| 久久影视精品| 日韩午夜精品| 久久精品av麻豆的观看方式| 欧美va天堂在线| 国产精品一二一区| **性色生活片久久毛片| 亚洲私人影院| 欧美ed2k| 亚洲专区国产精品| 欧美fxxxxxx另类| 国产精品综合不卡av| 亚洲国产日韩一区二区| 欧美一区成人| 亚洲巨乳在线| 久久精品中文| 国产精品女主播在线观看| 亚洲黑丝在线| 久久成人精品无人区| 亚洲激情视频在线播放| 性色av一区二区怡红| 欧美精品一区二区三区四区| 国内精品伊人久久久久av一坑| 夜夜夜久久久| 欧美不卡三区| 欧美一区二区国产| 国产精品初高中精品久久| 亚洲国产经典视频| 久久精品免费电影| 亚洲一区二区三区视频| 欧美美女日韩| 亚洲国产欧美日韩另类综合| 久久久久久久久综合| 亚洲视频第一页| 欧美精品一区二区蜜臀亚洲| 黄色精品一区| 久久久国产亚洲精品| 亚洲视频一区在线观看| 欧美日韩成人综合天天影院| 亚洲国产精品热久久| 久久久久久久久综合| 亚洲一区久久| 国产精品久久77777| 亚洲午夜伦理| 亚洲毛片在线观看.| 欧美大片91| 亚洲黄色天堂| 欧美福利网址| 免费成人小视频| 影音先锋中文字幕一区二区| 久久久久久穴| 久久成人这里只有精品| 国内成人精品视频| 久久综合色一综合色88| 久久久精品视频成人| 韩国欧美国产1区| 久久字幕精品一区| 久久久久免费视频| 亚洲第一黄色网| 欧美激情亚洲一区| 欧美日本一区二区高清播放视频| 亚洲人屁股眼子交8| 亚洲国产精品悠悠久久琪琪| 欧美成人激情视频免费观看| 亚洲欧洲精品成人久久奇米网| 欧美激情视频在线免费观看 欧美视频免费一| 久久久久久久一区二区| 亚洲国产精品女人久久久| 亚洲日本在线观看| 欧美日韩在线播放一区| 欧美一区二区成人6969|