只需要了解其中的几个方法就好
WriteStartDocument 方法 书写版本为“1.0”并具有独立属性的 XML 声明
- WriteComment 方法 注释
WriteStartElement 方法 写出指定的开始标记
WriteAttributeString 方法 写出具有指定值的属性
WriteElementString 方法 写出包含字符串值的元素
WriteEndElement 方法 结束标记
WriteEndDocument 方法 结束标记
上面有两个标记是一一对应的.最后都要结束标记.这样就算创建一个XML文件成功了.但会发现创建的XML文件全在一行显示.
这里就要用要XmlWriterSettings来设置XML功能了
1XmlWriterSettings settings = new XmlWriterSettings();
2settings.Indent = true;
3settings.ConformanceLevel = ConformanceLevel.Auto;
4settings.IndentChars = "\t";
5settings.OmitXmlDeclaration = false;
2settings.Indent = true;
3settings.ConformanceLevel = ConformanceLevel.Auto;
4settings.IndentChars = "\t";
5settings.OmitXmlDeclaration = false;
然后重载Create()方法就好了.这样创建的XML文件就不会写在一行了.
根据命名空间写入XML
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
string xmlFilePath = Request.PhysicalApplicationPath + @"\Employ.xml";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IndentChars = "\t";
settings.OmitXmlDeclaration = false;
try
{
using (XmlWriter writer = XmlWriter.Create(xmlFilePath,settings))
{
//书写声明
writer.WriteStartDocument(false);
//写入标记开始
writer.WriteStartElement("employees");
writer.WriteAttributeString("xmlns", "emp", null, "urn:employees-wrox");
writer.WriteStartElement("employee", "urn:employees-wrox");
/* 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)
{
lblResult.Text = "发生错误:" + ex.Message;
}
}
</script>
<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>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
string xmlFilePath = Request.PhysicalApplicationPath + @"\Employ.xml";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IndentChars = "\t";
settings.OmitXmlDeclaration = false;
try
{
using (XmlWriter writer = XmlWriter.Create(xmlFilePath,settings))
{
//书写声明
writer.WriteStartDocument(false);
//写入标记开始
writer.WriteStartElement("employees");
writer.WriteAttributeString("xmlns", "emp", null, "urn:employees-wrox");
writer.WriteStartElement("employee", "urn:employees-wrox");
/* 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)
{
lblResult.Text = "发生错误:" + ex.Message;
}
}
</script>
<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中
1<%@ Page Language="C#" %>
2
3<%@ Import Namespace="System.Xml" %>
4<%@ Import Namespace="System.IO" %>
5
6<script runat="server">
7 void Page_Load(object sender, EventArgs e)
8 {
9
10 string xmlFilePath = Request.PhysicalApplicationPath + @"\jpgexample.xml";
11 string imageFileName = Request.PhysicalApplicationPath + @"\Employee.jpg";
12 try
13 {
14 using (XmlWriter writer = XmlWriter.Create(xmlFilePath))
15 {
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 {
44 lblResult.Text = "发生错误: " + ex.Message;
45 }
46 }
47
48</script>
49
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
2
3<%@ Import Namespace="System.Xml" %>
4<%@ Import Namespace="System.IO" %>
5
6<script runat="server">
7 void Page_Load(object sender, EventArgs e)
8 {
9
10 string xmlFilePath = Request.PhysicalApplicationPath + @"\jpgexample.xml";
11 string imageFileName = Request.PhysicalApplicationPath + @"\Employee.jpg";
12 try
13 {
14 using (XmlWriter writer = XmlWriter.Create(xmlFilePath))
15 {
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 {
44 lblResult.Text = "发生错误: " + ex.Message;
45 }
46 }
47
48</script>
49
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
主要用到System.IO文件的操作.跟XML的操作没多大的关系