When you process XML documents in .NET, you often need to find the data of a node in the document. There are many ways to find a certain node. Here I will summarize several commonly used methods for you.
First, what we need to do is load an XML document into an XmlDocument object.
Let’s quote a few namespaces first:
using ;
using ;
using ;
Everyone knows the meaning of these name spaces based on their names, so I won’t say more here. Then the code to load the XML file is as follows:
String xmlfile="c:/"; //The xmlfile is the path of the XML file you want to load.
XmlDocument myDoc = new XmlDocument(); //Define an XmlDocument object.
(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></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 with name tim:
(1).(0).
This method requires us to look inwards inwardly. If there are many levels, it will be very difficult to do and make mistakes easily. Fortunately.NET provides us with another method SelectSingleNode and SelectNodes methods to allow us to directly find the data we want. For example, if we want to find hobby of the user named "Tim", we can use the following method:
("//member[name='Tim']").(1).InnerText
where // represents the child nodes of any layer inside. This way we can quickly find what we want. SelectSingleNode is to find a single node, and SelectNodes can find many nodes.
Everyone knows how to find a child node in XML. We are now looking for a child node in a special XML file--XSL file. How should this be implemented?
Suppose I now have an XSL file like this:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http:///1999/XSL/Transform" xmlns:fo="http:///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 it. We need XSL file to use these two variables when using Transform XML file. How should we do it?
The method I took is to load the XSL file as an XML Document first. Before using it, we find the node that needs to be modified and use our variables to modify it. At this time, we need to make some changes when looking for this node, the code is as follows:
XmlNamespaceManager nsmanager = new XmlNamespaceManager();
("xsl", "http:///1999/XSL/Transform");
("//xsl:attribute[@name='src']", nsmanager).InnerXml = variable you need to lose to
In other words, for nodes like <xsl:attribute name="src">../FTP_Magazine/FTP_Issue/<xsl:value-of select="@url"/></xsl:attribute>, before we look up, we need to define an XmlNamespaceManager, and we can use it to find the nodes we need.
First, what we need to do is load an XML document into an XmlDocument object.
Let’s quote a few namespaces first:
using ;
using ;
using ;
Everyone knows the meaning of these name spaces based on their names, so I won’t say more here. Then the code to load the XML file is as follows:
String xmlfile="c:/"; //The xmlfile is the path of the XML file you want to load.
XmlDocument myDoc = new XmlDocument(); //Define an XmlDocument object.
(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></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 with name tim:
(1).(0).
This method requires us to look inwards inwardly. If there are many levels, it will be very difficult to do and make mistakes easily. Fortunately.NET provides us with another method SelectSingleNode and SelectNodes methods to allow us to directly find the data we want. For example, if we want to find hobby of the user named "Tim", we can use the following method:
("//member[name='Tim']").(1).InnerText
where // represents the child nodes of any layer inside. This way we can quickly find what we want. SelectSingleNode is to find a single node, and SelectNodes can find many nodes.
Everyone knows how to find a child node in XML. We are now looking for a child node in a special XML file--XSL file. How should this be implemented?
Suppose I now have an XSL file like this:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http:///1999/XSL/Transform" xmlns:fo="http:///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 it. We need XSL file to use these two variables when using Transform XML file. How should we do it?
The method I took is to load the XSL file as an XML Document first. Before using it, we find the node that needs to be modified and use our variables to modify it. At this time, we need to make some changes when looking for this node, the code is as follows:
XmlNamespaceManager nsmanager = new XmlNamespaceManager();
("xsl", "http:///1999/XSL/Transform");
("//xsl:attribute[@name='src']", nsmanager).InnerXml = variable you need to lose to
In other words, for nodes like <xsl:attribute name="src">../FTP_Magazine/FTP_Issue/<xsl:value-of select="@url"/></xsl:attribute>, before we look up, we need to define an XmlNamespaceManager, and we can use it to find the nodes we need.