C # Read and Write XML files

Source: Internet
Author: User

XML () file,

The difference between XML and HTML is that XML is designed to transmit and store data, and its focus is on data content. HTML is designed to display data, with focus on the appearance of data. HTML is designed to display information, while XML is designed to transmit information.

Differences between XML and HTML Syntax: not all HTML tags need to appear in pairs. XML requires that all tags must appear in pairs. HTML tags are case-insensitive and XML is sensitive to size, it is case sensitive.

In Asp. Net, we need to use C # To read and write information about existing XML files. The Code is as follows:

// Create an instance and read XML XmlDocument xmldoc = new XmlDocument (); string path = HttpContext. current. server. mapPath (".. /XMLFile/ChatInfor. xml "); xmldoc. load (path); // find the root node XmlNode chat = xmldoc. selectSingleNode ("Chat"); // create the node XmlElement infor = xmldoc. createElement ("infor"); // you can specify the infor attribute. setAttribute ("id", mid); XmlElement xuser = xmldoc. createElement ("userid"); // insert text xuser. innerText = uid; infor. appendChild (xuser); XmlElement xdate = xmldoc. createElement ("date"); xdate. innerText = date; infor. appendChild (xdate); XmlElement xmess = xmldoc. createElement ("content"); xmess. innerText = mess; infor. appendChild (xmess); // save it to the root node and save the chat file. appendChild (infor); xmldoc. save (path );

PS: the mid, uid, date, and mess values in the preceding code are passed parameters;

The format of the stored XML file is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <Chat> <infor id = "71b6384d-0dad-62eb-c952-c718d33b3374"> <userid> 2 </userid> <date> 12:30:01 </date> <content> Hello, everyone! </Content> </infor> <infor id = "a179c895-9521-150e-2548-1872cfd2fd93"> <userid> 1 </userid> <date> 12:30:50 </date> <content> hello! </Content> </infor> <infor id = "2c0f2313-13af-f71b-f1a2-8b2e188d5536"> <userid> 2 </userid> <date> 15:05:23 </date> <content> 1235364564 </content>> </infor> <infor id = "d508830e-658f-2adf-88f6-4ef6d9295a88"> <userid> 1 </userid> <date> 15:05:30 </date> <content> ah </content> </infor> <infor id = "Courier"> <userid> 3 </userid> <date> 15:05:35 </date> <content> irrigation and filling </content> </infor> </Chat>

 

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.