Asp.net adds, deletes, modifies, and queries XML.

Source: Internet
Author: User
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. xml;
Private xmldocument xmldoc;

// Load XML file
Private void loadxml ()
{
Xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("user. xml "));
}

// Add a node
Private void addelement ()
{

Loadxml ();

Xmlnode xmldocselect = xmldoc. selectsinglenode ("user ");

Xmlelement El = xmldoc. createelement ("person"); // Add a person Node
El. setattribute ("name", ""); // Add the property of the person node "name"
El. setattribute ("sex", "female"); // Add the property of the person node "sex"
El. setattribute ("Age", "25"); // Add the property of the person node "Age"

Xmlelement xesub1 = xmldoc. createelement ("pass"); // Add a node in the person Node
Xesub1.innertext = "123"; // set a text node
El. appendchild (xesub1 );
Xmlelement xesub2 = xmldoc. createelement ("Address ");
Xesub2.innertext = "Kunming"; // set a text node
El. appendchild (xesub2 );

Xmldocselect. appendchild (EL );
Xmldoc. Save (server. mappath ("user. xml "));

}

// Modify a node
Private void updateelement ()
{
Loadxml ();
Xmlnodelist nodelist = xmldoc. selectsinglenode ("user"). childnodes; // obtain all the subnodes of the bookstore Node
Foreach (xmlnode Xn in nodelist) // traverses all subnodes
{
Xmlelement Xe = (xmlelement) xn; // converts the subnode type to the xmlelement type
If (Xe. getattribute ("name") = "") // If the attribute value of name is ""
{
Xe. setattribute ("name", "invention ");

// If there are subnodes below
Xmlnodelist NLS = Xe. childnodes; // continue to obtain all the child nodes of the Xe subnode
Foreach (xmlnode xn1 in NLS) // traverse
{
Xmlelement xe2 = (xmlelement) xn1; // Conversion Type
If (xe2.name = "pass") // If you find
{
Xe2.innertext = "66666"; // modify
Break;

}
}

Break;
}
}
Xmldoc. Save (server. mappath ("user. xml"); // save
}

// Delete a node
Private void deletenode ()
{

Loadxml ();
Xmlnodelist xnl = xmldoc. selectsinglenode ("user"). childnodes;

Foreach (xmlnode Xn in xnl)
{
Xmlelement Xe = (xmlelement) xn;

If (Xe. getattribute ("name") = "invention ")
{
// Xe. removeattribute ("name"); // delete the name attribute
Xe. removeall (); // delete all content of the node

Break;
}

}
Xmldoc. Save (server. mappath ("user. xml"); // save
}

Private void showit ()
{
Loadxml ();
Xmlnode xn = xmldoc. selectsinglenode ("user ");

Xmlnodelist xnl = xn. childnodes;

Foreach (xmlnode xnf in xnl)
{
Xmlelement Xe = (xmlelement) xnf;
// Console. writeline (Xe. getattribute ("name"); // display the attribute value
// Console. writeline (Xe. getattribute ("sex "));
//
// Xmlnodelist xnf1 = Xe. childnodes;
// Foreach (xmlnode xn2 in xnf1)
//{
// Console. writeline (xn2.innertext); // displays the child node text
//}

}

}

 

ASP. NET
Add, delete, modify, and query XML by: Admin Date:
Font size: medium or large
Click to download this file

User. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Userinfo>
<Datarow>
<Username> 333 </username>
& Lt; password & gt; 333 & lt;/password & gt;
<Study> 333 </study>
</Datarow>
</Userinfo>

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. xml;

Namespace xmldom
{
/// <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox textbox1;
Protected system. Web. UI. webcontrols. textbox textbox2;
Protected system. Web. UI. webcontrols. textbox textbox3;
Protected system. Web. UI. webcontrols. Button button1;
Protected system. Web. UI. webcontrols. DataGrid datagrid1;

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Readxml ();
}

Private void readxml ()
{
String url = server. mappath ("user. xml"); // obtain the XML file in the current folder
Streamreader sread = new streamreader (URL, system. text. encoding. getencoding ("gb2312"); // read characters from the byte stream with a specific encoding. garbled characters cannot be obtained only after being converted to gb2312.
Xmldatadocument datadoc = new xmldatadocument (); // operate XML documents
Datadoc. dataset. readxml (sread); // Save the read byte stream to dataset.
This. datagrid1.datasource = datadoc. dataset. Tables [0]. defaultview;
Datagrid1.datakeyfield = "username"; // create an index with Username
This. datagrid1.databind ();
Datadoc = NULL; // clear the XML data operation
Sread. Close (); // close byte stream reading

}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Maid + = new system. Web. UI. webcontrols. Fig cancelcommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid editcommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid updatecommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid deletecommand );
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void datagrid1_deletecommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
String url = server. mappath ("user. xml ");
String str_username = maid [E. Item. itemindex]. tostring ();
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (URL); // load the XML file
Xmlnode root = xmldoc. selectsinglenode ("userinfo"); // find the userinfo Node
Xmlnodelist xnl = root. childnodes; // obtain the subnode under userinfo
For (INT I = 0; I <xnl. Count; I ++)
{
Xmlnodelist xnl11 = xnl. Item (I). selectnodes ("username"); // find the node set under Username
Xmlelement Xe = (xmlelement) xnl11.item (0); // elements under the username Node
If (Xe. innertext = str_username) // gets the serial values of nodes and all subsets.
{

Root. removechild (Xe. parentnode); // remove the finger-top node
// Xe. parentnode. removechild (xe );
// Xnl. Item (I). removechild (xe );
// If (I <xnl. Count) I = I-1;
}
}
Xmldoc. Save (URL );
Readxml ();

}

Private void button#click (Object sender, system. eventargs E)
{
String url = server. mappath ("user. xml ");
String str_username = This. textbox1.text. tostring ();
String str_password = This. textbox2.text. tostring ();
String str_study = This. textbox3.text. tostring ();
Try
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (URL); // load the XML document
Xmlnode root = xmldoc. selectsinglenode ("userinfo"); // find the First Matching xmlnode
Xmlelement xe1 = xmldoc. createelement ("datarow"); // create the first element of the first node

Xmlelement xesub1 = xmldoc. createelement ("username"); // create attributes in the first element of the first node
Xesub1.innertext = str_username; // set the text node
Xe1.appendchild (xesub1); // Add the specified node to the end of the node's subnode list
Xmlelement xesub2 = xmldoc. createelement ("password ");
Xesub2.innertext = str_password;
Xe1.appendchild (xesub2 );
Xmlelement xesub3 = xmldoc. createelement ("study ");
Xesub3.innertext = str_study;
Xe1.appendchild (xesub3 );

Root. appendchild (xe1 );
Xmldoc. Save (URL); // Save the XML file

Readxml ();
}
Catch (exception ex)
{
Response. Write (ex. tostring ());
}

}

Private void datagrid1_editcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
This. datagrid1.edititemindex = E. Item. itemindex;
Readxml ();
}

Private void datagrid1_cancelcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
This. Maid =-1;
Readxml ();
}

Private void datagrid1_updatecommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
String url = server. mappath ("user. xml ");
String str_username = maid [E. Item. itemindex]. tostring ();

Textbox txt_username = (textbox) (E. Item. cells [2]. controls [0]);
Textbox txt_password = (textbox) (E. Item. cells [3]. controls [0]);
Textbox drp_study = (textbox) (E. Item. cells [4]. controls [0]);

Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (URL); // load the XML file
Xmlnode root = xmldoc. selectsinglenode ("userinfo"); // find the userinfo Node
Xmlnodelist xnl = root. childnodes; // obtain the subnode under userinfo
For (INT I = 0; I <xnl. Count; I ++)
{
Xmlnodelist xnl11 = xnl. Item (I). selectnodes ("username"); // find the node set under Username
Xmlelement Xe = (xmlelement) xnl11.item (0); // elements under the username Node
If (Xe. innertext = str_username) // gets the serial values of nodes and all subsets.
{
Xe. innertext = txt_username.text;
Xe. parentnode. childnodes. Item (1). innertext = txt_password.text;
Xe. parentnode. childnodes. Item (2). innertext = drp_study.text;
}

}
Xmldoc. Save (URL );
This. Maid =-1;
Readxml ();

}
}
}

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.