XML transfer WebServices data

xiaoxiao2021-03-05  21

Due to many advantages such as XML, XML technology is increasingly applied to enterprise data processing, such as applications, news release, accounting data processing, and more.

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.

Display XML file content

Using C # programming to display the information of the XML file in the XML file is to read the XML file content in a StreamReader class object using the standard class provided by the .NET, and read XML information to DataSet with the XMLDATADOCUMENT class DataSet, DataSet The DataView is assigned to a DataGrid on a web form. Finally, the data is displayed by DataBind, and the specific implementation code is as follows:

Using system.xml; // Processing XML must add Namespace, you also need to add system.xml.dllusing system.io; // read the XML file 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.xmlStreamReader tyj = new StreamReader (Server.MapPath (datafile)); XmlDatadocument datadoc = new XmlDatadocument (); // Create this object to read XmlDataDoc.DataSet.ReadXML (TYJ); // read guest.xml file content DataGrid1.datasource = datadoc.dataset.tables [0] .defaultView; // Set DataGrid Data Source DataGrid1.dataBind (); // 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 adds the following code:

effect code is When you press the Select button, execute the program in OnseTelectionName (), store the value (Name) in a single unit in the selected DataGrid into a session variable, and go to the next page.

Add XML file content

Add a corresponding several TextBox and a Button for Submit, as shown in the drawings of this article, as shown in the drawings of this article:

string datafile = "guest.xml" Xmldocument xmldocument = new Xmldocument () xmldocument.Load (Server.MapPath (datafile)) // read the guest.xml xmldocument in documentNavigator navigator = new documentNavigator (xmldocument) // most important class navigator.MoveTodocumentElement () navigator.Insert (System.Xml.TreePosition.FirstChild, XmlNodeType.Element, "Guest", "", "") // insert node Guestnavigator.Insert (System.Xml.TreePosition.FirstChild, XmlNodeType.Element , "Name", "", "") navigator.insert (System.xml.treePosition.firstchild, XmlNodetype.Text, "Name", "," ") navigator.value = name.text // assigns the node Navigator.movetoparent () // Returns the parent node guest ... // uses the same type of statement, inserting other elements such as country, e-mail address, and message under element NAM (Server.MAppath (DataFile)); / / Finally save this XML document navigator = null xmldocument = null // Release the 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 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 xpathnavigator.removeselected (); // Perform delete Xmldocument.save (Server.MAppath (DataFile )))); // Finally save this XML document navigator = null; // Release class xmlDocument = null // Release XML document, other programs can be cleared, using all information in XML files, use "Navigator.RemoveChildren () "" "Scheme can be realized.

in conclusion

In summary, C # write XML applications not only quickly and convenient, in writing ASP.NET database applications, replacing some small tables with XML files, reducing many data inventory, and allowing other network programs 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.

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

New Post(0)