C # reads and writes an XML file

Source: Internet
Author: User

XML (extensible Markup Language ) file, which can be used to tag data, define data types, is a source language that allows users to define their own markup language.

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

XML and HTML syntax differences: HTML markup is not all need to appear in pairs, the XML requires that all tags must appear in pairs; HTML tags are case-insensitive, XML is case-sensitive.

in ASP. C # We need to read and write information to an existing XML file, with the following code:

?
//创建实例,读取XML XmlDocument xmldoc = new XmlDocument(); string path = HttpContext.Current.Server.MapPath("../XMLFile/ChatInfor.xml"); xmldoc.Load(path); //查找根节点   XmlNode chat = xmldoc.SelectSingleNode("Chat"); //创建节点   XmlElement infor = xmldoc.CreateElement("infor"); //设置属性   infor.SetAttribute("id", mid);  XmlElement xuser = xmldoc.CreateElement("userid"); //插入文本   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); //存入根节点,保存文件   chat.AppendChild(infor); xmldoc.Save(path);

PS: The mid,uid,date,mess in the previous code is the passed parameter;

The XML file format is stored as follows:

?
<?xml version="1.0" encoding="utf-8"?><Chat>  <infor id="71b6384d-0dad-62eb-c952-c718d33b3374">    <userid>2</userid>    <date>2013-03-13 12:30:01</date>    <content>大家好!</content>  </infor>  <infor id="a179c895-9521-150e-2548-1872cfd2fd93">    <userid>1</userid>    <date>2013-03-13 12:30:50</date>    <content>你好!</content>  </infor>  <infor id="2c0f2313-13af-f71b-f1a2-8b2e188d5536">    <userid>2</userid>    <date>2013-05-27 15:05:23</date>    <content>1235364564</content>  </infor>  <infor id="d508830e-658f-2adf-88f6-4ef6d9295a88">    <userid>1</userid>    <date>2013-05-27 15:05:30</date>    <content>啊啊啊啊</content>  </infor>  <infor id="37cf60ad-76a6-2983-4a82-bfea197201cd">    <userid>3</userid>    <date>2013-05-27 15:05:35</date>    <content>灌灌灌灌灌</content>  </infor>  </Chat>

C # reads and writes an XML file

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.