XML document search use summary
Www.itonline.gd.cn 2003-11-19
When you handle XML documents in .NET, you often need to find data of a node in the document. To find a node, there are many ways, here I will summarize several common methods to everyone.
First, we have to do it to put an XML document into an XMLDocument object.
First quote a few namespaces:
USING SYSTEM.XML;
Using system.xml.xsl;
Using system.xml.xpath;
These names space everyone knows it according to the name, I will not say more here. Then the code to load the XML file, the method is 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 an XMLDocument object.
MyDoc.Load (XMLFile);
In this way, we have an XML document called MYDOC. We will now find some nodes in this document. Let's first look at the content of this XML file.
XML Version = "1.0" encoding = "UTF-8"?>
member>
member>
member>
member>
members>
We can now find Name Tim with the following method:
MyDoc.childNodes.Item (1) .childnodes.Item (0) .firstchild.innertext
This method requires our layer to find the data we need inward, if there is a lot of level, it will be very hard, and it is easy to make mistakes. Fortunately .NET gives us another method Selectsinglenode and SelectNodes method to let us find the data you want. For example, we are looking for hobby named "TIM", we can use the following method:
MyDoc.selectsinglenode ("//member[name=''tim''l']").childnodes.Item(1).innerText
Among them, // represents the child nodes of any layer inside. This way we can find what you want very quickly. SelectSinglenode is a single node that select Node can find a lot of nodes.
Looking for a child node in XML, everyone knows how to do it, we now go to a special XML file --- xsl file to find a child node, what should this be implemented?
Suppose I now have a such XSL file:
XML Version = "1.0" encoding = "GB2312"?>
xsl: template>