XML document search use summary

xiaoxiao2021-03-06  88

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.

TIM

reading

www.aspcool.com

Sandy

Learning

Shally

TRANLATING

Christine

Working

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:

>>>

{text ()}

../ ftp_magazine / ftp_issue /

We have two variables in ASP.NET, and we need the XSL file to use these two variables when the Transform XML file. How should we do it?

The way I take is to load the XSL file as XML Document. Before using, we find the node that needs to be modified, and modify it with our variables. At this time, we need to do some changes when you find 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) .INNNML = variable you need to lose

That is to say, for similar ../ ftp_magazine / ftp_issue /

Author: Eagle Source: aspxcn.com

转载请注明原文地址:https://www.9cbs.com/read-105759.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.057, SQL: 9