XmlTextWriter 类
表示提供快速、非缓存、只进方法的编写器,该方法生成包含 XML 数据(这些数据符合 W3C 可扩展标记语言 (XML) 1.0 和“XML 中的命名空间”建议)的流或文件。
引入命名空间:using Sysem.Xml
源程序:
输出结果:
表示提供快速、非缓存、只进方法的编写器,该方法生成包含 XML 数据(这些数据符合 W3C 可扩展标记语言 (XML) 1.0 和“XML 中的命名空间”建议)的流或文件。
引入命名空间:using Sysem.Xml
源程序:
1
using System;
2
using System.Xml;
3![](/Images/OutliningIndicators/None.gif)
4
public 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![](/Images/OutliningIndicators/InBlock.gif)
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
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
输出结果:
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>
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)