Web XML programming for Visual C #

Source: Internet
Author: User
Tags file system insert xpath
Visual|web|xml| programming because of the easy sharing of XML and many other advantages, XML technology is increasingly used in enterprise data processing and other fields, such as the application of enterprise reports, news releases, accounting data processing and so on.

XML is fast becoming a tool for transferring data from the middle tier to the desktop, since XML data can be integrated with a variety of backend (database) sources through a middle-tier broker, and most database manufacturers now have full support for XML technology, providing a variety of powerful capabilities for processing XML data.

Microsoft's. NET provides a powerful and fast development tool,--c#, in the core of XML, with unprecedented high development efficiency, especially in XML programming.

C # provides a number of related classes that handle XML data, such as classes that handle stream: XmlReader and XmlWriter; DOM classes: XmlNode, XmlDocument, and XmlElement; Class of XPath: Xmlnavigator;xslt class: XslTransform.

Displaying the contents of an XML file
The way to display the information in an XML file in C # programming is to read the contents of the XML file into a StreamReader class object using the standard classes provided by the. Net. Then using the dataset of XmlDataDocument class to read XML information to the dataset, the dataset is then assigned to a DataGrid on a Web Form in DataView, and finally the data is displayed by DataBind. The specific implementation code is as follows:

Using System.Xml;
Processing XML must add the namespace, also need to add in references System.XML.Dll
Using System.IO;
Read the XML file must add namespace

Then add the following code to the Page_Load:

protected void Page_Load (object sender, EventArgs e) {
String datafile= "Guest.xml";
Suppose the XML file is named 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.xml file contents
DataGrid1.DataSource = Datadoc. Dataset.tables[0]. DefaultView;
Setting the DataGrid data source
Datagrid1.databind ();
Binding
Datadoc=null;
Releasing resources
Tyj. Close ();}
Releasing the StreamReader class is very important, otherwise the next time you open it will show that the file is already in use

Corresponding to the functionality of the DataGrid in the Display Web form, we need to add the following function:

protected void Onselectname (Object Sender,eventargs e) {
session["Select_name"]= (String) datagrid1.selecteditem.cells[1]. Text.tostring ();
Stores the value (Name) of a cell in a row in a selected DataGrid in a session variable so that the next page
Response.Redirect ("xml_manage.aspx");}
Go to the Admin page with the Add Delete feature

The Web form adds the following code:

<asp:datagrid id=datagrid1 runat= "Server" onselectedindexchanged= "Onselectname"
<property name= "Columns"
<asp:buttoncolumn text= "Choose" commandname= "select"/>
</property>

The purpose of the code is to execute the program in Onselectname () after you press the "SELECT" button to save the value (Name) in one cell in a selected DataGrid into a session variable, and then go to the next page.

adding XML file Content
Add a few TextBox and a button for submission in the Web form, as shown in the figure in 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 to the XmlDocument.
Documentnavigator navigator = new Documentnavigator (XmlDocument);
The most important class
Navigator. Movetodocumentelement ();
Navigator. Insert (System.Xml.TreePosition.FirstChild, XmlNodeType.Element, "Guest", "", "");/Insert Node Guest
Navigator. Insert (System.Xml.TreePosition.FirstChild, XmlNodeType.Element, "Name", "", "");
Navigator. Insert (System.Xml.TreePosition.FirstChild, XmlNodeType.Text, "Name", "", "");
Navigator. Value=name.text;
Assigning values to this node
Navigator. Movetoparent ();
Return parent Node Guest
......
Using similar statements, insert other elements under the element name such as country, e-mail address, and message
XmlDocument. Save (Server.MapPath (datafile));
Finally save this XML document
Navigator=null;
Xmldocument=null;
Releases the XML document so that other programs can use it

The code above uses the Documentnavigator class to add elements and content, paying attention to releasing resources after use.

Delete XML file contents
Delete the selected record, and 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 to the XmlDocument.
Documentnavigator navigator = new Documentnavigator (XmlDocument);
Navigator. Movetodocumentelement ();
Navigator. Select ("/guests/guest[name= '" +session["Select_name"]+ "");
Parameter is XPath
Navigator. Removeselected ();
Perform deletes
XmlDocument. Save (Server.MapPath (datafile));
Finally save this XML document
Navigator=null;
Release class
Xmldocument=null;
Releases the XML document so that other programs can use it

If you need to clear all the information in the XML file, use the navigator. Removechildren (); " Statement can be implemented.

Conclusion
In a comprehensive sense, C # writing XML applications is fast and convenient, when writing ASP.net database applications, the use of XML files to replace some small table, can reduce many database access connections, but also make other network programs easier to use this data.

The main bottleneck of XML today is the reading or writing of the file system, therefore, you should use more memory and caching methods, if the amount of information is not huge and the amount of modification is small, and the volume of browsing, the use of XML method will be a good choice; on the contrary, if the volume of data is large, you should consider using a database that supports XML, Whether you use ado+ to connect to other databases or use SQL server,c#, there are direct class functions that let the information in these databases interact directly with XML.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.