Read the XML document with C #

xiaoxiao2021-03-06  101

This article will explain how to access an XML file using C # with a very simple example. There is no practical significance itself, it just briefly introduces how Microsoft's XML standards and how to use it in practice. I hope to be able to inspire the readers who have first contact C # or have not tried to read the XML file through C #. This article is designed to throw bricks, hoping to communicate and share experience with more friends.

Production process 

First, run Visual Studio.net, create a new Visual C # .NET project, here is READXML. Second, in the Solution Explorer, the FORM1.CS is renamed FRMAUTHOR.CS (this is optional). Third, change the FORM1's Name property to frMauthor, and the text property is changed to read XML Document. Font is changed to Verdana, 9pt (this is optional). Four, add four controls on the form: Lable Control (Name: LBL, Text: Author Name :), ComboBox Control (Name: CBOAUTHOR, DROPDOWNSTYLE: DROPDOWNLIST), RICHTEXTBOX control (Name: Richtxt, Text: Air), Button Control (Name: Btnshow, Text: show author's info). Adjust the location of each control, at which point the user interface is similar to the following figure:

5. Double-click the form, then the IDE automatically switches to the code editing window, and automatically adds the formal load method frMauthor_load. Add the following code within this method:

This.cboauthor.Items.add ("Zhang Love Bell"); this.cboauthor.Items.Add ("Fu Building"); this.cboauthor.items.Add ("Mark Tween");                                            The author name is in ComboBox, and the information of these authors has corresponding items in the XML document. OK, so far, some of the programs we have to do will have finished completed. Let us take a look at the main knowledge points to be displayed in this routine.

Before writing access to the code, let's take a look at the contents of the XML document, as follows:

- - omission - omission - omission This is a simple XML document, each author corresponds to a message (Intro), a total of three information. What we have to do is through the program, read the author's profile according to the structure of the XML document, which is the content of the INTRO section. OK, let us implement the specific code below.

First, place the above XML document (named Author.xml) under the root of the project (in fact, it can be placed anywhere, but in order to facilitate management, it is directly placed in the root directory of the project). Then return to VS.NET, add the following code in the head in the frmauthor.cs code:                                                                                                                                      Second, return to the frMauthor.cs [design] page, double-click the Show Author's INFO button, then the IDE automatically adds the btnshow_click event in frMauthor.cs. In the BTNSHOW_CLICK event, fill in the following code: try { // Declaration ^ String Strauthor = this.cboauthor.text.trim (); String strXmlauThor = ";" String strinfo;                                                                       Xmldocument ();   tem.xml.xpath.xpathnavigator nav;     = new system.xml.xpath.xpathnavigator () ;stem.xml.xpath.xpathNodeTeiterator Iterator; // = new system. Xml.xpath.xpathnodeiterator ();

                                                                       }

                            x x)

// SET NAV Object   nav = (System.xml.xPath.ixpathnavigable) (DOC)). Createnavigator ();

 // Justificationif (strAuthor == "Chang Ai-ling") strXMLAuthor = "Zhang"; else if (strAuthor == "Flaubert") strXMLAuthor = "Fu"; else if (strAuthor == "Mark Tween") strXmlauthor = "mark";

                                               

//Move to the desired node     ^ iterator.movenext ();

// Get the value of current node   Strinfo = Iterator.current.value;

                      } catch (                                                       

Third, the main code we have filled out. After using the menu "Generate"> Generate Solution command, press F5 to run the test program correct.

Interpretation

1, System.xml Namespace System.xml Namespace Provides standard support for processing XML. Supported standards include: • XML 1.0 - http://www.w3.org/TR/1998/Rec-xml-19980210 - including DTD support. · XML namespace - http://www.w3.org/tr/rec-xml-names/ - stream level and DOM. · XSD architecture - http://www.w3.org/2001/xmlschema · xpath expression - http://www.w3.org/tr/xpath · xslt conversion - http://www.w3.org/tr / xslt · DOM level 1 core - http://www.w3.org/tr/Rec-Dom-level-1/ · DOM level 2 core - http://www.w3.org/tr/Dom-level- 2/2, XPathnavigator class XPathnavigator is based on the XPath data model and provides the methods required to implement XPath queries on any data store. Xpathnavigator provides read-only random access to data. The current node is a node that is located on it. The navigator can be advanced using any mobile method and attribute reflects the value of the current node.

3, the XPathNodeTerrator class XPathNodeITOR class refers to the iterative program on a set of selected nodes. Iteractor is derived from Iterate (repeatedly, reaffirming, rearily reporting), which is an iterative search on the selected node, in our example, it looks for the conditional node and obtains the value on the node.

With regard to System.xml Namespace and its child namespace, you can check the MSDN Library after 2002. In the Namespace Hierarchy section, we can clearly see the hierarchies of each System.xml namespace.

The above is to show you how to use the C # and System.xml namespace to directly access an XML document directly with the C # and System.xml namespace. Of course, the code in this routine is just a way of implementation. The code has not been organized and optimized. Here, it is only hoped to give you a knowledge point of Visual C # through this routine. I hope I can provide more friends. Good comments and suggestions. Contact: xuyu@transtar.com.cn, personal website: http://www.epubcn.com, Forum: http://www.epubcn.net

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

New Post(0)