Asp.net operation XML
NET development, Asp. net Development article, Asp. net Information. asp.net operation XML. XML basic concepts 1. the XML document consists of two parts: 1. an XML document declares that this is an XML document 2. use XML to identify the created content 2. basic concepts in XML 1. element: the smallest unit of an XML document.
Http://csharp.xdowns.com/archives/2006/05/17/341.html
Asp.net reads, writes, adds, modifies, and deletes XML files.
The following code is correctly debugged:
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, Fengyun); // Add the property name of the person node.
El. setattribute (sex, female); // Add the property sex of the person Node
El. setattribute (age, 25); // Add the property age of the person node.
Xmlelement xesub1 = xmldoc. createelement (PASS); // Add the node in the person Node
Xesub1.innertext = 123; // set the text node
El. appendchild (xesub1 );
Xmlelement xesub2 = xmldoc. createelement (Address );
Xesub2.innertext = Kunming; // set the 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 name attribute value 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
//}
}
}
XML style:
<? XML version = 1.0 encoding = gb2312?>
<User>
<Person>
</Person>
<Person name = wind sex = male age = 25>
<Passed> 123 </pass>
<Address> Daming </address>
</Person>
<Person name = FY sex = female age = 25>
<Passed> 123 </pass>
<Address> Kunming </address>
</Person>
</User>