XML document search usage summary

Source: Internet
Author: User
Tags xsl file
When you process xml documents in. NET, you often need to find the data of a node in the document. There are many methods to find a node. here I will summarize several common methods. When you process xml documents in. NET, you often need to find the data of a node in the document. There are many methods to find a node. here I will summarize several common methods.

First, we need to load an XML document into an XmlDocument object.

Reference several namespaces first:

  using System.Xml;   using System.Xml.Xsl;   using System.Xml.XPath;


You will know the meaning of these namespaces based on their names, so I will not talk about them here. The code for loading the XML file is as follows:


String xmlfile = "c:/member. xml"; // The xmlfile is the path of the XML file to be loaded. XmlDocument myDoc = new XmlDocument (); // defines an XmlDocument object. MyDoc. Load (xmlfile );



In this way, we have an XML file named myDoc. Now let's look at some nodes in this document. Let's first look at the content of this XML file.

    
     
      
   
    Tim
       
   
    reading
       
   
    www.aspcool.com
       
      
      
   
    Sandy
       
   
    learning
       
      
      
   
    Shally
       
   
    tranlating
       
      
      
   
    Christine
       
   
    working
       
     
 


Now we can use the following method to find the node named tim:
MyDoc. ChildNodes. Item (1). ChildNodes. Item (0). FirstChild. InnerText

This method requires us to find the data we need in one layer. if there are many layers of data, it will be very difficult and prone to errors. Fortunately,. NET provides us with another method, SelectSingleNode and SelectNodes, so that we can directly find the desired data. For example, if we are looking for a user named "Tim", we can use the following method:
MyDoc. SelectSingleNode ("// member [name = 'Tim']"). ChildNodes. Item (1). InnerText

// Represents any layer of sub-nodes. In this way, we can quickly find what we want. SelectSingleNode is used to find a single node. SelectNodes can find many nodes.

We all know how to find a subnode in XML. how can we find a subnode in a special XML file, the XSL file?

Suppose I have an XSL file like this:

 
    
     
      
      
       
         
      
   
 
../FTP_Magazine/FTP_Issue/


We have two variables in asp.net. we need the XSL file to use these two variables in the Transform XML file. What should we do?

The method I used is to first load the XSL file as an XML Document. before using the file, we can find the node to be modified and modify it with our variables. At this time, we need to make some changes when searching for this node. the code is as follows:

XmlNamespaceManager nsmanager = new XmlNamespaceManager (invalid Doc. nameTable); nsmanager. addNamespace ("xsl", "http://www.w3.org/1999/XSL/Transform"); your doc. selectSingleNode ("// xsl: attribute [@ name = 'src']", nsmanager ). innerXml = the variable you need to lose


That is to say, ../FTP_Magazine/FTP_Issue/ Before we look for such a node, we need to define an XmlNamespaceManager and use it to find the node we need.

The above is the content of the XML document search usage summary. For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.