XML///< summary>
< author> flying Eagle @aspcool.com</author>
< description> This article describes the. NET to find the XML node. </desciption>
< copyright>asp Cool technology Information Network ( www.ASPCool.com) </copyright>
</summary>
Everybody in. NET, you often need to find the data for a node in your document when processing XML documents. There are a number of ways to find a node, where I'll summarize several common methods.
First, all we have to do is load an XML document into a XmlDocument object.
Refer to a few namespaces first:
Using System.Xml;
Using SYSTEM.XML.XSL;
Using System.Xml.XPath;
The names of these spaces are known by their names, and I will not be here to say more. Then there is the code that loads the XML file, as follows:
String xmlfile= "C:/member.xml"; The xmlfile is the path to the XML file you want to load.
XmlDocument myDoc = new XmlDocument (); Defines a XmlDocument object.
Mydoc.load (xmlfile);
In this way, we have an XML document called MyDoc. Let's look at some of the nodes in this document now. Let's look at the contents of this XML file first.
< XML version= "1.0" encoding= "UTF-8"?>
< members>
< member>
< Name>tim
< hobby>reading
< homepage>www.aspcool.com
</member>
< member>
< Name>sandy
< hobby>learning
</member>
< member>
< name>shally
< hobby>tranlating
</member>
< member>
< Name>christine
< hobby>working
</member>
</members>
We can now find the node named Tim in the following way:
MyDoc.ChildNodes.Item (1). Childnodes.item (0). Firstchild.innertext
This method requires us to look at the level of the data we need, if there are many levels, it will be difficult to do, but also error prone. Fortunately. NET provides us with another method selectSingleNode and SelectNodes methods can let us directly find the data we want. For example, we need to find the hobby of a user named "Tim", we can use the following method:
Mydoc.selectsinglenode ("//member[name= ' Tim ']"). Childnodes.item (1). InnerText
where//Represents the child nodes of any layer inside. So we can quickly find what we want. selectSingleNode is to find a single node, selectnodes can find many nodes.
Looking for a child node in XML, we all know how to do it, we are now in a special XML file---XSL file to find a child node, how should this be implemented?
Suppose I now have one of these XSL files:
< 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/
</img>
</td>
</tr>
< tr>
< td>
< center>
< xsl:apply-templates/>
</center>
</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
We have two variables in the ASP.net and we need the XSL file to take these two variables when we transform the XML file. How are we going to do that?
The approach I took was to load the XSL file as an XML document, and before we could use it, we found the nodes that needed to be modified and modified them with our variables. This time we need to make some changes when we look up 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 = ' src ']", Nsmanager). INNERXML = The variable you need to lose
In other words, for a similar. /ftp_magazine/ftp_issue/Such a node, before we look, we need to define a XmlNamespaceManager, with which we can find the nodes we need.
Author: Flying Eagle/aspcool.com