Simple application of XML in. Net

Source: Internet
Author: User
Tags add date date1 datetime net net xml string tostring
Xml

. NET XML


1.XML is a markup language that provides a description of the data format. Its self description makes it very adaptable to data exchange between different applications, and this exchange is not based on a predefined group data structure!

eg

<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<course_information>
<courseName> Operating System </courseName>
<courseTeacher> Zou </courseTeacher>
<coursePath>Database.aspx</coursePath>
<courseAddTime>2006-7-1</courseAddTime>
</course_information>
<course_information>
<courseName> Database System Principles </courseName>
<courseTeacher> Hu </courseTeacher>
<coursePath>Database.aspx</coursePath>
<courseAddTime>2006-7-2</courseAddTime>
</course_information>
<course_information>
<courseName> Data Structure </courseName>
<courseTeacher> Lee </courseTeacher>
<coursePath>Database.aspx</coursePath>
<courseAddTime>2006-7-30</courseAddTime>
</course_information>
</root>

2. Edit XML Document

First read the course_information.xml into memory, form a DOM tree, and then locate the parent node that you want to insert, and add a new

Node to add a new node, which is a new element of the XML document. Finally, the DOM tree in memory is saved as an XML document.

eg

To add a new node:

String st= "database.aspx";
DateTime Date1=datetime.now;
String date=date1. ToString ("D");
XmlDocument xmldoc=new XmlDocument ();
Xmldoc.load (file);
XmlNode root=xmldoc.selectsinglenode ("root");//select the root node;
XmlElement course=xmldoc.createelement ("course_information");
XmlElement name=xmldoc.createelement ("Coursename");
Name. Innertext=this. TextBox1.Text;
XmlElement teacher=xmldoc.createelement ("Courseteacher");
Teacher. Innertext=this. TextBox2.Text;
XmlElement path=xmldoc.createelement ("Coursepath");
Path. innertext=st;
XmlElement addtime=xmldoc.createelement ("Courseaddtime");
Addtime.innertext=date;
Course. AppendChild (name);
Course. AppendChild (teacher);
Course. AppendChild (path);
Course. AppendChild (Addtime);
Root. AppendChild (course);
Xmldoc.save (file);
Xmldoc=null;

To delete a node:

String Filename=server.mappath (@ ". \xml\course_information.xml");
XmlDocument xmldoc=new XmlDocument ();
Xmldoc.load (filename);
String Name=this. TextBox1.Text.ToString ();
XmlNode oldnode=xmldoc.selectsinglenode ("Root/course_information[coursename= '" +name+ ")";
XmlNode root=xmldoc.selectsinglenode ("root");
Root. RemoveChild (OldNode);
Xmldoc.save (filename);
Xmldoc=null;
Dt=op. READXML (filename);
Dv=dt. DefaultView;
Bindgrid ()//bind to DataGrid




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.