Net Operation XML class

Source: Internet
Author: User

<? XML version = "1.0"?>
<Userlogin>
<User>
<Usercode> 001 </usercode>
<Username> operator 1 </username>
<Userpwd> 111 </userpwd>
</User>
<User>
<Usercode> 002 </usercode>
<Username> operator 2 </username>
<Userpwd> 222 </userpwd>
</User>
</Userlogin>

 

 

 

Class:

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. xml;

/** // <Summary>
/// Read/write class for XML files
/// </Summary>
///
Public class xmlrw
{
Public xmlrw ()
{
//
// Todo: add the constructor logic here
//
}

// Writexml completes adding the user
// The location where the current XML file is stored in Filename
// Usercode: encode the user to be added
// Username: name of the user to be added
// Userpassword

Public void writexml (string filename, string usercode, string username, string userpassword)
{

// Initialize the XML document operation class
Xmldocument mydoc = new xmldocument ();
// Load the XML file
Mydoc. Load (filename );

// Add the element -- usercode
Xmlelement ele = mydoc. createelement ("usercode ");
Xmltext text = mydoc. createtextnode (usercode );

// Add the element -- username
Xmlelement ele1 = mydoc. createelement ("username ");
Xmltext text1 = mydoc. createtextnode (username );

// Add the element -- userpwd
Xmlelement ele2 = mydoc. createelement ("userpwd ");
Xmltext text2 = mydoc. createtextnode (userpassword );

// Add the node user to correspond to the node name in our XML file
Xmlnode newelem = mydoc. createnode ("element", "user ","");

// Add an element to a node
Newelem. appendchild (Ele );
Newelem. lastchild. appendchild (text );
Newelem. appendchild (ele1 );
Newelem. lastchild. appendchild (text1 );
Newelem. appendchild (ele2 );
Newelem. lastchild. appendchild (text2 );

// Add the node to the document
Xmlelement root = mydoc. documentelement;
Root. appendchild (newelem );

// Save
Mydoc. Save (filename );

}

// Deletenode completes adding a user
// The location where the current XML file is stored in Filename
// Usercode: encode the user to be added

Public void deletenode (string filename, string usercode)
{
// Initialize the XML document operation class
Xmldocument mydoc = new xmldocument ();
// Load the XML file
Mydoc. Load (filename );

// Search for a specified column, which is generally a primary key column
Xmlnodelist mynode = mydoc. selectnodes ("// usercode ");

// Determine whether this node exists

If (! (Mynode = NULL ))
{
// Traverse the node and find the qualified elements

Foreach (xmlnode Xn in mynode)
{
If (Xn. innerxml = usercode)
// Delete the parent node of an element
XN. parentnode. parentnode. removechild (Xn. parentnode );
}
}

// Save
Mydoc. Save (filename );

}

// Writexml completes password modification for the user
// The location where the current XML file is stored in Filename
// User code
// Userpassword

Public void updatexml (string filename, string usercode, string userpassword)
{

// Initialize the XML document operation class
Xmldocument mydoc = new xmldocument ();
// Load the XML file
Mydoc. Load (filename );

// Search for a specified Node
System. xml. xmlnodelist nodes = mydoc. selectnodes ("// user ");

If (nodes! = NULL)
{
Foreach (system. xml. xmlnode Xn in nodes)
{
If (Xn. selectsinglenode ("usercode"). innertext = usercode)
{
XN. selectsinglenode ("userpwd"). innertext = userpassword;
}

}
}

Mydoc. Save (filename );

}

}

Usage:

Protected void button#click (Object sender, eventargs E)
{

// Add reference and create instance
Xmlrw myxml = new xmlrw ();
// Call the method we have defined to specify parameters.
Myxml. writexml (server. mappath ("ytconfig. xml"), textbox1.text, textbox2.text, textbox3.text );

Response. Write ("Save OK! ");

}
Protected void button2_click (Object sender, eventargs E)
{
Xmlrw myxml = new xmlrw ();
Myxml. deletenode (server. mappath ("ytconfig. xml"), textbox1.text );

Response. Write ("delete OK! ");

}
Protected void button3_click (Object sender, eventargs E)
{
Xmlrw myxml = new xmlrw ();
Myxml. updatexml (server. mappath ("ytconfig. xml"), textbox1.text, textbox3.text );

Response. Write ("update OK! ");

}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fishpowersoft/archive/2007/12/21/1957113.aspx

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.