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='src']", nsmanager).InnerXml = 你所需要输给的变量
In other words, for something like
The above is the summary of XML document search usage. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!