XML file: xmlfile. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Smallfoolsroot>
<Poems>
<ID> 1 </ID>
<Author> Wang Wei </author>
<Title> Bamboo Pavilion </title>
<Content> sit in the room alone and play the piano. Shen Lin does not know, the moon to photograph. </Content>
</Poems>
<Poems>
<ID> 3 </ID>
<Author> Li Bai </author>
<Title> Tuling's sentence </title>
<Content> it is located on the nandeng Tuling and is located in the north of the 5 th mausoleum. The autumn water sets the sunset and goes out of the distant mountains. </content>
</Poems>
<Poems>
</Smallfoolsroot>
// Operation 1: Read the entire XML file
Dataset DS = new dataset ();
DS. readxml (server. mappath ("xmlfile/xmlfile. xml "));
If (Ds. Tables. Count> 0)
{
This. repeater1.datasource = Ds. Tables [0]. defaultview;
This. repeater1.databind ();
}
// Operation 2: obtain the value of a single node
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("xmlfile/xmlfile. xml "));
Xmlnodelist = xmldoc. selectnodes ("smallfoolsroot/poems [ID = '" + request. querystring ["Xid"]. tostring () + "']"); // search
Xmlnode = xmlnodelist. Item (0 );
This. tbauthor. Text = xmlnode ["author"]. innertext;
This. tbtitle. Text = xmlnode ["title"]. innertext;
This. tbcontent. Text = xmlnode ["content"]. innertext;
Public String returncount ()
{
// Return the last id value and + 1
String I = string. empty;
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("xmlfile/xmlfile. xml "));
Xmlnode = xmldoc. documentelement. lastchild;
If (xmlnode! = NULL)
{
I = convert. tostring (convert. touint32 (xmlnode ["ID"]. innertext) + 1 );
}
Return I;
}
// Operation 4: Add a node
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("xmlfile/xmlfile. xml "));
// Create a new node
Xmlelement newelement = xmldoc. createelement ("Poems ");
// Create a node under newelement
Xmlelement ELID = xmldoc. createelement ("ID ");
Xmlelement elauthor = xmldoc. createelement ("author ");
Xmlelement eltitle = xmldoc. createelement ("title ");
Xmlelement elcontent = xmldoc. createelement ("content ");
ELID. innertext = returncount ();
Elauthor. innertext = This. tbaddauthor. Text. Trim ();
Eltitle. innertext = This. tbaddtitle. Text. Trim ();
Elcontent. innertext = This. tbaddcontent. Text. Trim ();
// Add the node under newelement to newelement
Newelement. appendchild (ELID );
Newelement. appendchild (elauthor );
Newelement. appendchild (eltitle );
Newelement. appendchild (elcontent );
// Add newelement to the XML file (add it to the last record)
Xmldoc. documentelement. appendchild (newelement );
// If You Want To insert a record, you can also use it (after the first record)
// Xmldoc. documentelement. insertafter (newelement, xmldoc. documentelement. childnodes. Item (0 ));
// You can also use it before inserting a record (before the first record)
// Xmldoc. documentelement. insertbefore (newelement, xmldoc. documentelement. childnodes. Item (0 ));
// Save the disk
Xmldoc. Save (server. mappath ("xmlfile/xmlfile. xml "));
// Operation 5: delete a node
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("xmlfile/xmlfile. xml "));
Xmlnodelist = xmldoc. selectnodes ("smallfoolsroot/poems [ID = '" + request. querystring ["Xid"]. tostring () + "']"); // search
Xmlnode = xmlnodelist. Item (0 );
Xmlnode. parentnode. removechild (xmlnode );
Xmldoc. Save (server. mappath ("xmlfile/xmlfile. xml "));
// Operation 6: edit a node
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("xmlfile/xmlfile. xml "));
// Obtain the node list
Xmlnodelist = xmldoc. selectnodes ("smallfoolsroot/poems [ID = '" + request. querystring ["Xid"]. tostring () + "']"); // search
Xmlnode = xmlnodelist. Item (0 );
Xmlnode ["author"]. innertext = This. tbauthor. text;
Xmlnode ["title"]. innertext = This. tbtitle. text;
Xmlnode ["content"]. innertext = This. tbcontent. text;
Xmldoc. Save (server. mappath ("xmlfile/xmlfile. xml "));
Usage:
1. The location of the server. mappath ("/") application root directory is c: \ Inetpub \ wwwroot \
2. server. mappath ("./") indicates the current directory of the page
Note: It is equivalent to the physical file path on the page where server. mappath ("") is returned.
3. server. mappath ("../") indicates the directory at the upper level.
4. server. mappath ("~ /") Indicates the directory of the current application. If it is the root directory, it is the root directory. If it is a virtual directory, it is the location of the virtual directory, such as: C: \ Inetpub \ wwwroot \ example \
Note: It is equivalent to server. mappath ("~ ").
The most common XML data types are: element, attribute, comment, text.
Element, which indicates the <Name> Tom <Name> node. It can include: element, text, comment, processinginstruction, CDATA, and entityreference.
Attribute, which is the bold part in <employee ID = "12345">.
Comment, pointing to: <! -- My comment --> node.
Text, which is in bold of <Name> Tom <Name>.
In XML, you can use xmlnode objects to refer to various XML data types.
2.1 query nodes (sets) with known absolute paths)
Objnodelist = objdoc. selectnodes ("Company/department/employees/employee ")
Or
Objnodelist = objnode. selectnodes ("/Company/department/employees/employee ")
The preceding two methods return a nodelist object. to return a single node, use the selectsinglenode method. If one or more nodes are queried by this method, the first node is returned; if no node is queried, return nothing
2.2 query nodes (sets) with known relative paths)
You can use a relative path similar to the file path to query XML data.
Objnode = objdoc. selectsinglenode ("Company/department ")
Objnodelist = objnode. selectnodes ("../Department)
Objnode = objnode. selectnode ("employees/employee ")
2.3 query nodes (sets) with known element names)
When using an irregular hierarchical document, you can use the // symbol to query its child, sun, or all other elements in the multi-level document because you do not know the element name of the intermediate level. For example:
Objnodelist = objdoc. selectnodes ("company // employee ")
2.4 query attribute nodes
All the above methods return element nodes (sets) and attribute. You only need to use the corresponding method and add a @ symbol before the attribute name. For example:
Objnodelist = objdoc. selectnodes ("Company/department/employees/employee/@ ID ")
Objnodelist = objdoc. selectnodes ("company // @ ID ")
2.5 query text nodes
Use text () to obtain the text node.
Objnode = objdoc. selectsinglenode ("Company/department/deparmt_name/text ()")
2.6 query nodes with specific conditions
Use the [] symbol to query nodes with specific conditions. For example:
A. Return the employee node with ID 10102.
Objnode = objdoc. selectsinglenode ("Company/department/employees/employee [@ ID = '000000']")
B. Return the Name node whose name is Zhang Qi.
Objnode = objdoc. selectsinglenode ("Company/department/employees/employee/name [text () = 'zhang qi']")
C. Return the Department Name node with 22345 employees
Objnode = objdoc. selectsinglenode ("Company/Department [employees/employee/@ ID = '000000']/department_name ")
2.7 query nodes in multiple modes
Use the | symbol to obtain nodes in multiple modes. For example:
Objnodelist = objdoc. selectnodes ("Company/department/department_name | Company/department/manager ")
2.8 Query any subnode
You can use the * symbol to return all the subnodes of the current node.
Objnodelist = objdoc. selectnodes ("Company/*/Manager)
Or
Objnodelist = objnode. childnodes
4. Reference Data
<? XML version = "1.0" encoding = "UTF-8"?>
<Company>
& Lt; department ID = "101" & gt;
<Department_name> Cai wubu </department_name>
<Manager> Zhang Bin </Manager>
<Employees>
<Employee ID = "12345">
<Employee_id> 12345 </employee_id>
<Name> Zhang Bin </Name>
<Gender> male </gender>
</Employee>
<Employee ID = "10101">
<Employee_id> 10101 </employee_id>
<Name> Zhang Qi </Name>
<Gender> female </gender>
</Employee>
<Employee ID = "10102">
<Employee_id> 10102 </employee_id>
<Name> zhang xia </Name>
<Gender> male </gender>
</Employee>
<Employee ID = "10201">
<Employee_id> 10201 </employee_id>
<Name> zhangchuang </Name>
<Gender> male </gender>
</Employee>
<Employee ID = "10202">
<Employee_id> 10202 </employee_id>
<Name> Zhang Jun </Name>
<Gender> male </gender>
</Employee>
</Employees>
</Department>
& Lt; department ID = "102" & gt;
<Department_name> kaifa Bu </department_name>
<Manager> Wang Bin </Manager>
<Employees>
<Employee ID = "22345">
<Employee_id> 22345 </employee_id>
<Name> Wang Bin </Name>
<Gender> male </gender>
</Employee>
<Employee ID = "20101">
<Employee_id> 20101 </employee_id>
<Name> Wang Qi </Name>
<Gender> female </gender>
</Employee>
<Employee ID = "20102">
<Employee_id> 20102 </employee_id>
<Name> Wang Xia </Name>
<Gender> male </gender>
</Employee>
<Employee ID = "20201">
<Employee_id> 20201 </employee_id>
<Name> Wang Chuang </Name>
<Gender> male </gender>
</Employee>
<Employee ID = "20202">
<Employee_id> 20201 </employee_id>
<Name> Wang Jun </Name>
<Gender> male </gender>
</Employee>
</Employees>
</Department>
</Company>