XmlTextWriter 类
表示提供快速、非缓存、只进方法的编写器,该方法生成包含 XML 数据(这些数据符合 W3C 可扩展标记语言 (XML) 1.0 和“XML 中的命名空间”建议)的流或文件。
引入命名空间:using Sysem.Xml
源程序:
输出结果:
表示提供快速、非缓存、只进方法的编写器,该方法生成包含 XML 数据(这些数据符合 W3C 可扩展标记语言 (XML) 1.0 和“XML 中的命名空间”建议)的流或文件。
引入命名空间:using Sysem.Xml
源程序:
1using System;
2using System.Xml;
3
4public partial class _Default : System.Web.UI.Page
5{
6 protected void Button1_Click(object sender, EventArgs e)
7 {
8 XmlTextWriter writer = null;
9 try
10 {
11 writer = new XmlTextWriter(Server.MapPath("First.xml"),System.Text.Encoding.UTF8);
12 writer.Formatting = Formatting.Indented;
13
14 writer.WriteStartDocument();
15 writer.WriteStartElement("Teachers");
16 writer.WriteStartElement("Teacher");
17 writer.WriteAttributeString("number","firstone");
18 writer.WriteElementString("name","John");
19 writer.WriteElementString("Nianling","12");
20 writer.WriteElementString("sex","woman");
21 writer.WriteStartElement("xueli", "gao");
22 writer.WriteAttributeString("ninn", "11212");
23 writer.WriteEndElement();
24 writer.WriteElementString("xueke", "shuxue");
25 writer.WriteEndElement();
26 writer.WriteEndElement();
27 }
28 finally
29 {
30 if (writer != null)
31 writer.Close();
32 }
33 }
34}
2using System.Xml;
3
4public partial class _Default : System.Web.UI.Page
5{
6 protected void Button1_Click(object sender, EventArgs e)
7 {
8 XmlTextWriter writer = null;
9 try
10 {
11 writer = new XmlTextWriter(Server.MapPath("First.xml"),System.Text.Encoding.UTF8);
12 writer.Formatting = Formatting.Indented;
13
14 writer.WriteStartDocument();
15 writer.WriteStartElement("Teachers");
16 writer.WriteStartElement("Teacher");
17 writer.WriteAttributeString("number","firstone");
18 writer.WriteElementString("name","John");
19 writer.WriteElementString("Nianling","12");
20 writer.WriteElementString("sex","woman");
21 writer.WriteStartElement("xueli", "gao");
22 writer.WriteAttributeString("ninn", "11212");
23 writer.WriteEndElement();
24 writer.WriteElementString("xueke", "shuxue");
25 writer.WriteEndElement();
26 writer.WriteEndElement();
27 }
28 finally
29 {
30 if (writer != null)
31 writer.Close();
32 }
33 }
34}
输出结果:
1<?xml version="1.0" encoding="utf-8"?>
2<Teachers>
3 <Teacher number="firstone">
4 <name>John</name>
5 <Nianling>12</Nianling>
6 <sex>woman</sex>
7 <xueli ninn="11212" xmlns="gao" />
8 <xueke>shuxue</xueke>
9 </Teacher>
10</Teachers>
2<Teachers>
3 <Teacher number="firstone">
4 <name>John</name>
5 <Nianling>12</Nianling>
6 <sex>woman</sex>
7 <xueli ninn="11212" xmlns="gao" />
8 <xueke>shuxue</xueke>
9 </Teacher>
10</Teachers>