Asp.net simple operations on XML files

Source: Internet
Author: User

Before starting, create a smallfools. xml file with the following content:
<? 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 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 xmlNodeList = xmlDoc. DocumentElement. ChildNodes;
XmlNode 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 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 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 () + "')]");

Related Article

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.