• <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>

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

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久综合给久久狠狠97色| 国产免费久久久久久无码| 亚洲国产另类久久久精品小说 | 91精品国产乱码久久久久久| 国产69精品久久久久APP下载 | 久久免费的精品国产V∧| 久久一日本道色综合久久| 99久久久精品免费观看国产| 国产精品美女久久久免费| 久久99精品国产麻豆宅宅| 精品久久无码中文字幕| 久久亚洲精品国产亚洲老地址| 久久人人爽人人爽人人片av麻烦| 日韩av无码久久精品免费| 久久亚洲精品中文字幕三区| 亚洲欧美成人久久综合中文网| 久久精品国产亚洲AV无码娇色| 韩国三级中文字幕hd久久精品| 久久青青草原精品国产| 99久久亚洲综合精品成人| 性欧美丰满熟妇XXXX性久久久| 人人狠狠综合久久亚洲高清| 久久精品麻豆日日躁夜夜躁| 一本色道久久88综合日韩精品 | 国内精品人妻无码久久久影院导航| 亚洲狠狠综合久久| 午夜久久久久久禁播电影| 香蕉99久久国产综合精品宅男自 | 久久婷婷人人澡人人爽人人爱 | 无码国内精品久久综合88| 久久91这里精品国产2020| 久久人人爽人人爽人人AV东京热 | 久久福利片| 亚洲欧美日韩精品久久| 久久99国产精品一区二区| 久久婷婷五月综合97色| 无码AV波多野结衣久久| 久久精品国产亚洲av影院| AV色综合久久天堂AV色综合在| 国产精品久久久久久一区二区三区| 久久亚洲精品人成综合网|