<? XML version = "1.0" encoding = "UTF-8"?>
<Smallfoolsroot>
<Poems>
<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>
<Author> Meng Haoran </author>
<Title> sujian Dejiang </title>
<Content> migrate the boat to smoke, and the guests are worried about new things. Ye Kuang, tianshu, Jiang qingyue </content>
</Poems>
<Poems>
<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>
<Author> Li Bai </author>
<Title> wanglushan waterfall </title>
<Content> the sunshine incense burner produces Ziyan, And the waterfall goes down to qianchuan. Flying down three thousand feet, suspected to be nine days of Galaxy. </Content>
</Poems>
<Poems>
<Author> Li Shangyin </author>
<Title> Jin Se </title>
<Content> Jin Se has fifty strings for no reason, one string and one column of Si Hua Nian. Zhuang Sheng Xiaomeng fan butterfly, hope the Emperor spring heart to trust the cuckoo. There are tears in the pearl of the sea and the moon. This situation can be recalled, but it was not clear at the time. </Content>
</Poems>
</Smallfoolsroot>
The following operations are performed in the XML file.
Operation 1: Read the entire XML file and display it in the DataGrid:
Dataset DS = new dataset ();
DS. readxml (server. mappath ("smallfools. xml "));
If (Ds. Tables. Count> 0)
{
This. datagrid1.datasource = Ds. Tables [0]. defaultview;
This. datagrid1.databind ();
}
Operation 2: obtain the value of the first node
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("smallfools. xml "));
Xmlnode = xmldoc. documentelement. firstchild;
If (xmlnode! = NULL)
{
This. tbauthor. Text = xmlnode ["author"]. innertext;
This. tbtitle. Text = xmlnode ["title"]. innertext;
This. tbcontent. Text = xmlnode ["content"]. innertext;
Viewstate ["count"] = 0;
}
Operation 3: view the content of a node
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("smallfools. xml "));
Xmlnodelist = xmldoc. documentelement. childnodes;
Xmlnode = xmlnodelist. Item (0 );
This. tbauthor. Text = xmlnode ["author"]. innertext;
This. tbtitle. Text = xmlnode ["title"]. innertext;
This. tbcontent. Text = xmlnode ["content"]. innertext;
Operation 4: Add a node
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("smallfools. xml "));
// Create a new node
Xmlelement newelement = xmldoc. createelement ("Poems ");
// Create a node under newelement
Xmlelement elauthor = xmldoc. createelement ("author ");
Xmlelement eltitle = xmldoc. createelement ("title ");
Xmlelement elcontent = xmldoc. createelement ("content ");
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 (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 ("smallfools. xml "));
Operation 5: delete a node
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("smallfools. xml "));
Xmlnode = xmldoc. documentelement. childnodes. Item (0 );
Xmlnode. parentnode. removechild (xmlnode );
Xmldoc. Save (server. mappath ("smallfools. xml "));
Operation 6: edit a node
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("smallfools. xml "));
// Obtain the node list
Xmlnode = xmldoc. documentelement. childnodes. Item (1 );
Xmlnode ["author"]. innertext = This. tbauthor. text;
Xmlnode ["title"]. innertext = This. tbtitle. text;
Xmlnode ["content"]. innertext = This. tbcontent. text;
Xmldoc. Save (server. mappath ("smallfools. xml "));
Operation 7: query records
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("smallfools. xml "));
Xmlnodelist nodelist = xmldoc. selectnodes ("smallfoolsroot/poems [author = '" + this. tbsearch. Text. Trim () + "']");
Operation 8: paste mode query record
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("smallfools. xml "));
Xmlnodelist nodelist = xmldoc. selectnodes ("smallfoolsroot/poems [contains (author, '" + this. tbsearch. Text. Trim () + "')]");