C # xml manipulation

Source: Internet
Author: User

Due to XML's ease of sharing and many other advantages, XML technology is increasingly used in enterprise data processing and other fields, such as enterprise reports, news releases, and accounting data processing.
XML is quickly becoming a tool for transferring data from the middle layer to the desktop. Because XML data can be integrated with multiple backend (database) sources through the middle layer proxy, most database manufacturers currently fully support XML technology, provides various powerful functions to process XML data.
Microsoft's. NET provides a powerful and fast development tool-C #, which features unprecedented development efficiency, especially in XML programming.
C # provides many related classes to process XML data, such as stream processing classes: xmlreader and xmlwriter; DOM classes: xmlnode, xmldocument, and xmlelement; XPath classes: xmlnavigator; XSLT class: xsltransform.
 

Show XML file content
C # programming is used to display information in XML files. the standard class provided by net reads XML file content into a streamreader class object, and then reads XML Information to dataset using the xmldatadocument class dataset to read XML Information, dataset is assigned to a DataGrid on the web form in the form of dataview, and databind displays the data. The specific implementation code is as follows:
Using system. xml;
// The namespace required for processing XML. You must also add system. xml. dll in references.
Using system. IO;
// Namespace required for reading XML files
Then add the following code to page_load:
Protected void page_load (Object sender, eventargs e ){
String datafile = "guest. xml ";
// Assume that the XML file name is guest. xml.
Streamreader tyj = new streamreader (server. mappath (datafile ));
Xmldatadocument datadoc = new xmldatadocument ();
// Create this object to read XML
Datadoc. dataset. readxml (tyj );
// Read the content of the guest. xml file
Datagrid1.datasource = datadoc. dataset. Tables [0]. defaultview;
// Set the DataGrid Data Source
Datagrid1.databind ();
// Bind
Datadoc = NULL;
// Release resources
Tyj. Close ();}
// Release the streamreader class, which is very important; otherwise, the file will be displayed as used next time.
Corresponding to the DataGrid function in the web form used for display, we need to add the following function:
Protected void onselectname (Object sender, eventargs e ){
Session ["select_name"] = (string) datagrid1.selecteditem. cells [1]. Text. tostring ();
// Store the value (name) in a unit in a row of the selected DataGrid into a session variable for the next page
Response. Redirect ("xml_manage.aspx ");}
// Go to the Management page with the deletion function added
Add the following code to web form:
<Asp: DataGrid id = maid = "server" onselectedindexchanged = "onselectname">
<Property name = "columns">
<Asp: buttoncolumn text = "select" commandname = "select"/>
</Property>
The purpose of the underlined part in the code is to execute the program in onselectname () after clicking the "select" button, and set the value (name) in a unit in a row in the selected DataGrid) store a session variable and go to the next page.

 

Add XML file content
Add four labels (names, sources, email addresses, and message content), four textbox and a button for submission in the web form, and add the following code for the button:
String datafile = "guest. xml"; xmldocument = new xmldocument ();
Xmldocument. Load (server. mappath (datafile ));
// Read guest. XML to xmldocument
Documentnavigator navigator = new documentnavigator (xmldocument );
// The most important class
Navigator. movetodocumentelement ();
Navigator. insert (system. xml. treeposition. firstchild, xmlnodetype. element, "guest", "", ""); // Insert the node guest
Navigator. insert (system. xml. treeposition. firstchild, xmlnodetype. element, "name ","","");
Navigator. insert (system. xml. treeposition. firstchild, xmlnodetype. Text, "name ","","");
Navigator. value = "/name. Text ";
// Assign a value to the node
Navigator. movetoparent ();
// Return the parent node guest
......
// Use similar statements to insert other elements such as country, email address, and message under element name
Xmldocument. Save (server. mappath (datafile ));
// Finally save the XML document
Navigator = NULL;
Xmldocument = NULL;
// Release the XML document so that other programs can use it
The above Code uses the documentnavigator class to add elements and content. Pay attention to releasing resources after use.
Delete XML file content
Delete the selected record. For the Node Selected above, the following code finds the node and clears the selected information:
String datafile = "guest. xml ";
Xmldocument = new xmldocument ();
Xmldocument. Load (server. mappath (datafile ));
// Read guest. XML to xmldocument
Documentnavigator navigator = new documentnavigator (xmldocument );
Navigator. movetodocumentelement ();
Navigator. Select ("/guests/guest [name =" "+ session [" select_name "] +" "]");
// The parameter is XPath.
Navigator. removeselected ();
// Execute Delete
Xmldocument. Save (server. mappath (datafile ));
// Finally save the XML document
Navigator = NULL;
// Release class
Xmldocument = NULL;
// Release the XML document so that other programs can use it
To clear all information in the XML file, use the "Navigator. removechildren ();" statement.

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.