这次继续学习XML编写器.直接看代码吧.比较简单
1
<%@ Page Language="C#" %>
2![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
3
<%@ Import Namespace="System.Xml" %>
4![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
5
<script runat="server">
6
void Page_Load(object sender, EventArgs e)
7![](https://i-blog.csdnimg.cn/blog_migrate/3a1c2d778e58280a03608ca7c2e471fc.gif)
{
8
string xmlFilePath = Request.PhysicalApplicationPath + @"/Employee.xml";
9
try
10![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
{
11
using (XmlWriter writer = XmlWriter.Create(xmlFilePath))
12![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
{
13
//书写声明
14
writer.WriteStartDocument(false);
15
//写入注释
16
writer.WriteComment("此XML文件第一次被创建");
17
//写入标记
18
19
writer.WriteStartElement("employees");
20
writer.WriteStartElement("employee");
21
//写入标记属性和属性值
22
writer.WriteAttributeString("id", "1");
23
24
writer.WriteStartElement("name");
25
writer.WriteElementString("firstName", "Nancy");
26
writer.WriteElementString("lastName", "lastName");
27
//结素name标记
28
writer.WriteEndElement();
29
30
writer.WriteElementString("city", "Seattle");
31
writer.WriteElementString("state", "WA");
32
writer.WriteElementString("zipCode", "98122");
33
//结束employee标记
34
writer.WriteEndElement();
35
writer.WriteEndElement();
36
writer.WriteEndDocument();
37
//刷新基础流
38
writer.Flush();
39
lblResult.Text = "写入成功";
40
}
41
}
42
catch (Exception ex)
43![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
{
44
lblResult.Text = "发生错误:" + ex.Message;
45
}
46
}
47![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
48
</script>
49![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
50
<html xmlns="http://www.w3.org/1999/xhtml">
51
<head runat="server">
52
<title>Writing XML File</title>
53
</head>
54
<body>
55
<form id="form1" runat="server">
56
<div>
57
<asp:Label ID="lblResult" runat="server" />
58
</div>
59
</form>
60
</body>
61
</html>
62
只需要了解其中的几个方法就好
WriteStartDocument 方法 书写版本为“1.0”并具有独立属性的 XML 声明
然后重载Create()方法就好了.这样创建的XML文件就不会写在一行了.
根据命名空间写入XML
<%
@ Page Language
=
"
C#
"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
<%
@ Import Namespace
=
"
System.Xml
"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
<
script runat
=
"
server
"
>
void
Page_Load(
object
sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/3a1c2d778e58280a03608ca7c2e471fc.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
string xmlFilePath = Request.PhysicalApplicationPath + @"/Employ.xml";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IndentChars = "/t";
settings.OmitXmlDeclaration = false;
try
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
{
using (XmlWriter writer = XmlWriter.Create(xmlFilePath,settings))
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
{
//书写声明
writer.WriteStartDocument(false);
//写入标记开始
writer.WriteStartElement("employees");
writer.WriteAttributeString("xmlns", "emp", null, "urn:employees-wrox");
writer.WriteStartElement("employee", "urn:employees-wrox");
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
/**//* You can also use this approach to declare the namespace
string prefix = writer.LookupPrefix("urn:employees-wrox");
writer.WriteStartElement(prefix, "employee", null);
*/
writer.WriteAttributeString("id", "1");
writer.WriteStartElement("name", "urn:employees-wrox");
writer.WriteElementString("firstName", "urn:employees-wrox", "Nancy");
writer.WriteElementString("lastName", "urn:employees-wrox", "lastName");
writer.WriteEndElement();
writer.WriteElementString("city", "urn:employees-wrox", "Seattle");
writer.WriteElementString("state", "urn:employees-wrox", "WA");
writer.WriteElementString("zipCode", "urn:employees-wrox", "98122");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
lblResult.Text = "写入成功";
}
}
catch (Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
{
lblResult.Text = "发生错误:" + ex.Message;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
</
script
>
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
Writing XML File
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
<
asp:Label ID
=
"
lblResult
"
runat
=
"
server
"
/>
</
div
>
</
form
>
</
body
>
</
html
>
简单的就是重载其中的几个方法就可以了
最后一个例子
将数据以二进制的方式写入XML中
主要用到System.IO文件的操作.跟XML的操作没多大的关系
![](https://i-blog.csdnimg.cn/blog_migrate/4968dcdbdfc6cfa5e041e6385303b345.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3a1c2d778e58280a03608ca7c2e471fc.gif)
1
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/3a1c2d778e58280a03608ca7c2e471fc.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4968dcdbdfc6cfa5e041e6385303b345.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
22
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
23
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
24
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
25
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
26
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
27
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
28
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
29
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
30
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
31
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
32
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
33
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
34
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
35
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
36
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
37
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
38
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
39
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
40
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
41
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
42
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
43
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
44
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
45
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
46
![](https://i-blog.csdnimg.cn/blog_migrate/de2bbefebbaa11a78e36c375d8376464.gif)
47
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
48
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
49
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
50
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
51
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
52
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
53
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
54
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
55
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
56
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
57
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
58
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
59
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
60
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
61
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
62
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
只需要了解其中的几个方法就好
WriteStartDocument 方法 书写版本为“1.0”并具有独立属性的 XML 声明
-
WriteComment 方法 注释
WriteStartElement 方法 写出指定的开始标记
WriteAttributeString 方法 写出具有指定值的属性
WriteElementString 方法 写出包含字符串值的元素
WriteEndElement 方法 结束标记
WriteEndDocument 方法 结束标记
上面有两个标记是一一对应的.最后都要结束标记.这样就算创建一个XML文件成功了.但会发现创建的XML文件全在一行显示.
这里就要用要XmlWriterSettings来设置XML功能了
1
XmlWriterSettings settings
=
new
XmlWriterSettings();
2
settings.Indent
=
true
;
3
settings.ConformanceLevel
=
ConformanceLevel.Auto;
4
settings.IndentChars
=
"
/t
"
;
5
settings.OmitXmlDeclaration
=
false
;
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
然后重载Create()方法就好了.这样创建的XML文件就不会写在一行了.
根据命名空间写入XML
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3a1c2d778e58280a03608ca7c2e471fc.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4968dcdbdfc6cfa5e041e6385303b345.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/de2bbefebbaa11a78e36c375d8376464.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
简单的就是重载其中的几个方法就可以了
最后一个例子
将数据以二进制的方式写入XML中
1
<%
@ Page Language
=
"
C#
"
%>
2![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
3
<%
@ Import Namespace
=
"
System.Xml
"
%>
4
<%
@ Import Namespace
=
"
System.IO
"
%>
5![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
6
<
script runat
=
"
server
"
>
7
void
Page_Load(
object
sender, EventArgs e)
8![](https://i-blog.csdnimg.cn/blog_migrate/3a1c2d778e58280a03608ca7c2e471fc.gif)
{
9![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
10
string xmlFilePath = Request.PhysicalApplicationPath + @"/jpgexample.xml";
11
string imageFileName = Request.PhysicalApplicationPath + @"/Employee.jpg";
12
try
13![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
{
14
using (XmlWriter writer = XmlWriter.Create(xmlFilePath))
15![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
{
16
17
writer.WriteStartDocument(false);
18
writer.WriteStartElement("employee");
19
writer.WriteAttributeString("id", "1");
20
writer.WriteStartElement("image");
21
writer.WriteAttributeString("fileName", imageFileName);
22
//获取文件大小
23
FileInfo fi = new FileInfo(imageFileName);
24
int size = (int)fi.Length;
25
//读取文件
26
byte[] imgBytes = new byte[size];
27
FileStream stream = new FileStream(imageFileName, FileMode.Open);
28
BinaryReader reader = new BinaryReader(stream);
29
imgBytes = reader.ReadBytes(size);
30
reader.Close();
31
//写入数据
32
//将指定的二进制字节编码为 BinHex 并写出结果文本
33
writer.WriteBinHex(imgBytes, 0, size);
34
writer.WriteEndElement();
35
writer.WriteEndElement();
36
writer.WriteEndDocument();
37
38
writer.Flush();
39
lblResult.Text = "写入成功";
40
}
41
}
42
catch (Exception ex)
43![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
{
44
lblResult.Text = "发生错误: " + ex.Message;
45
}
46
}
47![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
48
</
script
>
49![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
50
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
51
<
head runat
=
"
server
"
>
52
<
title
>
Writing Images
using
XmlWriter
</
title
>
53
</
head
>
54
<
body
>
55
<
form id
=
"
form1
"
runat
=
"
server
"
>
56
<
div
>
57
<
asp:Label ID
=
"
lblResult
"
runat
=
"
server
"
/>
58
</
div
>
59
</
form
>
60
</
body
>
61
</
html
>
62
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/3a1c2d778e58280a03608ca7c2e471fc.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4968dcdbdfc6cfa5e041e6385303b345.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
22
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
23
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
24
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
25
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
26
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
27
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
28
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
29
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
30
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
31
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
32
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
33
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
34
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
35
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
36
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
37
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
38
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
39
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
40
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
41
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
42
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
43
![](https://i-blog.csdnimg.cn/blog_migrate/d956d23f92c7381a20bee108a2099f73.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d47f466770b50db01aa9765767f6b5dd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
44
![](https://i-blog.csdnimg.cn/blog_migrate/3efa1f9bd41368461325541070f19ce1.gif)
45
![](https://i-blog.csdnimg.cn/blog_migrate/6edbf03e95f55230124c993fcb753174.gif)
46
![](https://i-blog.csdnimg.cn/blog_migrate/de2bbefebbaa11a78e36c375d8376464.gif)
47
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
48
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
49
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
50
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
51
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
52
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
53
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
54
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
55
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
56
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
57
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
58
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
59
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
60
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
61
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
62
![](https://i-blog.csdnimg.cn/blog_migrate/a11078bdc01a5771ce53e056951850ea.gif)
主要用到System.IO文件的操作.跟XML的操作没多大的关系