XML is an Extensible Markup language, in electronic computers, marking refers to the computer can understand the information symbols, through such a mark, the computer can be processed with a variety of information such as articles. It can be used to tag data, define data types, and is a source language that allows users to define their own markup language. It is ideal for world Wide Web transport, providing a unified approach to describing and exchanging structured data that is independent of the application or vendor. is a cross-platform, content-dependent technology in the Internet environment and an effective tool for dealing with distributed structure information today.
Now demonstrate how to manipulate read and write XML files in ASP.
1. Create a new Web Form WebForm1.aspx and an XML file Xmlfile1.xml
(1) WebForm1.aspx page Code
Place two buttons, one for writing, one for reading, and one lable control to display read information
<%@ Page language="C #"autoeventwireup="true"Codebehind="WebForm1.aspx.cs"inherits="Read and write XML files. WebForm1"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div> <asp:button id="Button1"runat="Server"text="Write"onclick="Button1_Click"/> <asp:button id="Write"runat="Server"text="Read"onclick="Write_click"/> </div> <asp:label id="Label1"runat="Server"></asp:Label> </form></body>View Code(2) WebForm1.aspx.cs Background Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Xml;namespaceRead and write XML file { Public Partial classWebForm1:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { } protected voidButton1_Click (Objectsender, EventArgs e) { //Write XML//path to write XML stringPath = Server.MapPath ("Xmlfile1.xml"); //Create a file format ObjectXmlTextWriter XML =NewXmlTextWriter (path, System.Text.Encoding.UTF8); Xml. WriteStartDocument ();//Call the WriteStartDocument () method to begin writing to the XML documentXML. WriteStartElement ("the most beautiful rating");//Write root nodeXML. WriteStartElement ("Student");//Write child nodesXml. WriteStartElement ("Zhang San");//Writing NodesXml. WriteAttributeString ("Color","Red");//Add attributes to nodes (as if useless)Xml. WriteString ("Name: Zhang San"); Xml. WriteString ("<br/> Remarks: I ' m a boy");//add text to the inside of a nodeXml. WriteEndElement ();//Close byte PointXml. WriteEndElement ();//Close child nodesXML. WriteStartElement ("Student"); //Adds a single byte point to add node content at the same time through WriteElementStringXml. WriteElementString ("name","Mink Cicada"); Xml. WriteElementString ("Notes","I ' m a gril"); Xml. WriteEndElement (); Xml. WriteStartElement ("Student"); Xml. WriteElementString ("name","Lu BU"); Xml. WriteElementString ("Notes","I ' m a MR"); Xml. WriteEndElement (); //It is easier to add node content than to build a node firstXml. WriteStartElement ("Student"); Xml. WriteElementString ("Stuno","1002"); Xml. WriteElementString ("Stuname","CV"); //Student label closing labelXML. WriteEndElement (); Xml. WriteEndElement ();//Close the root nodeXml. Flush ();//Refresh StreamXml. Close ();//Close the stream } protected voidWrite_click (Objectsender, EventArgs e) {//Read stringPath = Server.MapPath ("Xmlfile1.xml");//Read Path//Create a file format ObjectXmlTextReader xml =NewXmlTextReader (Path);//Call Path intindex =1;//Indexed Values Student intA =1; while(XML. Read ()) {if(XML. NodeType = =xmlnodetype.element) {if(XML. Name = ="Student") { This. Label1.Text + ="Student"+ Index +"The information is as follows: <br/>"; Index++; } if(XML. Name = ="Stuno") { This. Label1.Text + ="School Number:"+ XML. Readelementstring () +"<br/>"; } if(XML. Name = ="Stuname") { This. Label1.Text + ="Name:"+ XML. Readelementstring () +"<br/>"; } if(XML. Name = ="name") { This. Label1.Text + ="Name:"+ XML. Readelementstring () +"<br/>"; } if(XML. Name = ="Notes")//the node name in parentheses { This. Label1.Text + ="remark:"+ XML. Readelementstring () +"<br/>"; } if(XML. Name = ="Zhang San")//the node name in parentheses { This. Label1.Text + =""+ XML. Readelementstring () +"<br/>"; } } } } }}
View CodeIt is worth noting that the writestartelement of each node in the background code should correspond to a writeendelement, and the hierarchical relationship should be distinct, although there is no error when you write, but it may not be what you want when you read it.
(3) Xmlfile1.xml
The new XML file is indicated in addition to the head file, and the rest is blank.
(4) Running the program
Note that you first have to write the data to the XML file in order to read the data
After clicking the Submit button, do not rush to the point to read the button, return to the program will have such an interface
Click Yes to update the data in the XML
The following is the updated Xmlfile1.xml file
1<?xml version="1.0"encoding="Utf-8"?>2< The most beautiful appraisal >3<student>4< Zhang San color="Red"> Name: Zhang San <br/> remarks: I'm a boy</Zhang San >5</student>6<student>7< name > Sable cicada </name >8< remarks >i'm a gril</notes >9</student>Ten<student> One< name > BU </name > A< remarks >i'm a mr</notes > -</student> -<student> the<stuNO>1002</stuNO> -<stuName>cv</stuName> -</student> -</Best Rating >
View CodeTurn off the light after writing the tired lie!!
Asp. NET write and read XML files