Visual C # Web XML programming
Reposted: Chi0591 Date: 2003-12-08 Popularity: 20 Due to many advantages such as XML, XML technology is increasingly applied to enterprise data processing, such as applications, press release, accounting data Processing, etc.
XML is quickly transmitted from the intermediate layer to the desktop tool. Since XML data can be integrated with multiple backend (database) sources through the intermediate layer agent, most database manufacturers have fully supported XML technology, providing each Powerful function to process XML data.
Microsoft's .NET offers a powerful and fast development tool around XML - C #, which has unprecedented high development efficiency, especially in XML programming.
C # provides many related classes to process XML data, such as class: XMLReader and XMLWriter; DOM class: XMLNode, XmLDocument and XMLELEMENT, etc. Class: Xmlnavigator; xslt class: xsltransform.
Displaying XML file content With C # programming implementation of display XML files in the XML file is to read the contents of the XML file in a StreamReader class object using the standard class provided by .NET, read XML information to the XML information to read XML information with the XMLDATADOCUMENT class. DataSet, DataSet is assigned to a DataGrid on a Web Form in a DataView, and finally displays data by DataBind, the specific implementation code is as follows:
Using system.xml; // Processing XML must be added Namespace, you need to plus system.xml.dll useing system.io; // read XML file must be added to Namespace in References
Then add the following code in Page_Load:
protected void Page_Load (object sender, EventArgs e) {string datafile = "guest.xml"; // assume an XML file called guest.xml StreamReader tyj = new StreamReader (Server.MapPath (datafile)); XmlDataDocument datadoc = new XmlDataDocument ( ); // Create this object To read XML DATADOC.DataSet.Readxml (TYJ); // Read guest 1.xml file content DataGrid1.datasource = datadoc.dataset.tables [0] .defaultView; // Set DataGrid data source DataGrid1.database (); // Bind DATADOC = NULL; // Release Resource Tyj.close ();} // Release the StreamReader class, which is very important, otherwise the next open will display the file has been used.
For the function of DataGrid, we need to increase the following functionality corresponding to the displayed Web Form:
Protected Void OnsectName (Object Sender, Eventargs E) {session ["SELECT_NAME"] = (String) DataGrid1.selectedItem.cells [1] .text.toString (); // In a single unit in selected DataGrid The value (Name) is stored in a session variable so that the next page uses response.redirect ("XML_Manage.aspx");} // Go to the management page with the increase of deletion
Web Form adding the following code:
The role of the code is that when the "Select" button is pressed, execute the programs in OnseTelectionName (), put the value (name) in a single unit in the selected DataGrid into a session variable, then go to the next One page.
Increasing the XML file content Add a corresponding several TextBox and a Button for commit, as shown in the drawings of this article, and add the following code for this Button:
string datafile = "guest.xml"; XmlDocument xmldocument = new XmlDocument (); xmldocument.Load (Server.MapPath (datafile)); // read the guest.xml XmlDocument in DocumentNavigator navigator = new DocumentNavigator (xmldocument); // The most important class navigator.movetodocumentelement (); navigator.insert (System.xml.treePosition.firstchild, XmlNodetype.Element, "Guest", "," "); // Insert Node Guest Navigator.Isert (System.xml. TreePosition.firstchild, XMLNodetype.element, "Name", "", ""); Navigator.Insert (System.xml.treePosition.FirstChild, XMLNodetype.Text, "Name", "," "); Navigator.Value = Name.text; // assigns Navigator.movetoparent (); // Returns the parent node guest ... // Using the same type of statement, inserting the other elements such as country, e-mail addresses, etc. XMLDocument. Save (server.mappath (datafile)); // Finally save this XML document navigator = null; xmlDocument = null; // Release XML document, so other programs can be used
The above code uses the Documentnavigator class to add elements and content, pay attention to release resources after use.
Delete the XML file content Delete the selected record, for the node you selected above, the following code can find the node and clear the selected information:
string datafile = "guest.xml"; XmlDocument xmldocument = new XmlDocument (); xmldocument.Load (Server.MapPath (datafile)); // read the guest.xml XmlDocument in DocumentNavigator navigator = new DocumentNavigator (xmldocument); navigator. MoveTodocumentelement (); navigator.select ("/ guests / guest [name = '" session ["SELECT_NAME"] ""); // parameter is xpath navigator.removeselected (); // Execute Delete XMLDocument.save (Server.MAppath); // Finally save this XML document navigator = null; // Release class xmlDocument = null; // Release the XML document, other programs can be used to clear all the information in the XML file, Use "Navigator.removeChildren ();" statement.
Conclusion, C # writes XML applications not only quickly and easily, when writing ASP.NET database applications, replacing some small Table with XML files, reducing many data inventory, can also make other network programs more easy to use These data.
At present, the main bottleneck of XML is reading or writing of the file system, so it should use more memory and cache methods. If the amount of information is not huge, the modified amount is small, and the XML method will be very Good choice;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; [ Interaction visit.