Home  >  Article  >  Backend Development  >  Summary of using XML document search

Summary of using XML document search

黄舟
黄舟Original
2017-03-03 17:06:201887browse

When we process xml documents in .NET, we often need to find the data of a certain node in the document. There are many ways to find a certain node. Here I will summarize a few commonly used methods for you.

First, what we have to do is to load an XML document into an XmlDocument object.

Let’s quote a few namespaces first:

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


Everyone knows the meaning of these namespaces based on their names, so I won’t say more here. . Then there is the code to load the XML file. The method is as follows:


String xmlfile="c:/member.xml"; //其中的xmlfile是你要载入的XML文件的路径。 
  XmlDocument myDoc = new XmlDocument(); //定义一个XmlDocument对象。 
  myDoc.Load(xmlfile);



In this way, we have an XML document called myDoc. Let's look for some nodes in this document now. Let's first look at the contents of this XML file.

<?xml version="1.0" encoding="UTF-8"?> 
  <members> 
   <member> 
   <name>Tim</name> 
   <hobby>reading</hobby> 
   <homepage>www.aspcool.com</homepage> 
   </member> 
   <member> 
   <name>Sandy</name> 
   <hobby>learning</hobby> 
   </member> 
   <member> 
   <name>Shally</name> 
   <hobby>tranlating</hobby> 
   </member> 
   <member> 
   <name>Christine</name> 
   <hobby>working</hobby> 
   </member> 
  </members>


We can now use the following method to find the node named tim:
myDoc.ChildNodes.Item(1).ChildNodes.Item(0).FirstChild .InnerText

This method requires us to look inward layer by layer to find the data we need. If there are many layers, it will be very laborious and error-prone. Fortunately, .NET provides us with another method SelectSingleNode and SelectNodes methods that allow us to directly find the data we want. For example, if we want to find the hobby of the user named "Tim", we can use the following method:
myDoc.SelectSingleNode ("//member[name='Tim']").ChildNodes.Item(1). InnerText

Where // represents the child node of any layer inside. This way we can find what we are looking for quickly. SelectSingleNode finds a single node, and SelectNodes can find many nodes.

Everyone knows how to find a child node in XML. Now we are looking for a child node in a special XML file---XSL file. How should this be achieved?

Suppose I now have an XSL file like this:

 <?xml version="1.0" encoding="gb2312"?> 
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
   <xsl:PReserve-space elements="codes"/> 
   <xsl:template match="/"> 
   <xsl:apply-templates/> 
   </xsl:template> 
   
   <xsl:template match="image"> 
   <table align="{@location}"> 
   <tr> 
   <td> 
   <img align="{@location}" alt="{text()}"> 
   <xsl:attribute name="src">../FTP_Magazine/FTP_Issue/<xsl:value-of select="@url"/></xsl:attribute> 
   </img> 
   </td> 
   </tr> 
   <tr> 
   <td> 
   <center> 
   <xsl:apply-templates/> 
   </center> 
   </td> 
   </tr> 
   </table> 
   </xsl:template> 
     </xsl:stylesheet>


We have two variables in asp.net, and we need the XSL file to be in Transform XML These two variables are used when creating files. How do we do this?

The method I took is to first load the XSL file as an XML Document. Before using it, we find the node that needs 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(xslDoc.NameTable); 
  nsmanager.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform"); 
  xslDoc.SelectSingleNode("//xsl:attribute[@name=&#39;src&#39;]", nsmanager).InnerXml = 你所需要输给的变量


In other words, for something like c6790cd964aa3a1ac5ae76cecbdc246d. ./FTP_Magazine/FTP_Issue/0b4405061552d20d6a74f9c944f8d93b74840d2e8628076055d931bf84be95ea Before we search, we need to define an XmlNamespaceManager, using which we can find The nodes we need.

The above is the summary of XML document search usage. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn