asp.net读取Xml文件中的数据
1.新建一个Xml文件,如下,在里面存放数据
1 <?xml version="1.0" encoding="utf-8" ?>
2 <Entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
4 <Title>Hello!</Title>
5 <Details>These are the details of the Weblog entry</Details>
6 </Entry>
2 <Entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
4 <Title>Hello!</Title>
5 <Details>These are the details of the Weblog entry</Details>
6 </Entry>
2. 新建一个Entry类,与Xml文件交互,代码如下
1 using System;
2 using System.IO;
3 using System.Xml.Serialization;
4
5 namespace _1asp
6 {
7 public class Entry
8 {
9 private DateTime _timestamp;
10 private String _title;
11 private String _details;
12
13
14 [XmlIgnore()]
15 public DateTime Timestamp
16 {
17 get
18 {
19 return _timestamp;
20 }
21 set
22 {
23 _timestamp = value;
24 }
25 }
26
27 public String Title
28 {
29 get
30 {
31 return _title;
32 }
33 set
34 {
35 _title = value;
36 }
37 }
38
39 public String Details
40 {
41 get
42 {
43 return _details;
44 }
45 set
46 {
47 _details = value;
48 }
49 }
50 }
51 }
2 using System.IO;
3 using System.Xml.Serialization;
4
5 namespace _1asp
6 {
7 public class Entry
8 {
9 private DateTime _timestamp;
10 private String _title;
11 private String _details;
12
13
14 [XmlIgnore()]
15 public DateTime Timestamp
16 {
17 get
18 {
19 return _timestamp;
20 }
21 set
22 {
23 _timestamp = value;
24 }
25 }
26
27 public String Title
28 {
29 get
30 {
31 return _title;
32 }
33 set
34 {
35 _title = value;
36 }
37 }
38
39 public String Details
40 {
41 get
42 {
43 return _details;
44 }
45 set
46 {
47 _details = value;
48 }
49 }
50 }
51 }
3.添加一个GLobal.asax
用来存放全局变量(本例子中为xml所在的文件目录),同时添加一个静态方法LoadEntry,代码如下(加标注的地方为手动添加,其余大部分为自动生成)
1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.SessionState;
6 using System.IO;
7 using System.Xml.Serialization;
8
9 namespace _1asp
10 {
11 public class Global : System.Web.HttpApplication
12 {
13 //成员
14 public static String EntryFilePath;
15
16 protected void Application_Start(object sender, EventArgs e)
17 {
18 EntryFilePath = Server.MapPath("Entries");
19 }
20
21 protected void Session_Start(object sender, EventArgs e)
22 {
23
24 }
25
26 protected void Application_BeginRequest(object sender, EventArgs e)
27 {
28
29 }
30
31 protected void Application_AuthenticateRequest(object sender, EventArgs e)
32 {
33
34 }
35
36 protected void Application_Error(object sender, EventArgs e)
37 {
38
39 }
40
41 protected void Session_End(object sender, EventArgs e)
42 {
43
44 }
45
46 protected void Application_End(object sender, EventArgs e)
47 {
48
49 }
50
51 /*加载XML数据文件,并把内容放在定义的Entry类中
52 * 使用了System.Xml.Serialization名称空间中的一个类XmlSerializer,调用它的方法Deserialize()
53 * 并把它的数据转换为一组对象属性,它们存储在Entry类的实例中
54 */
55 public static Entry LoadEntry(String filename)
56 {
57 String filepath = EntryFilePath + "\\" + filename;
58 FileStream file = new FileStream(filepath, FileMode.Open);
59
60 XmlSerializer serializer = new XmlSerializer(typeof(Entry));
61 Entry newEntry = (Entry)serializer.Deserialize(file);
62
63 file.Close();
64 //显示时间
65 newEntry.Timestamp = new FileInfo(filepath).LastWriteTime;
66 return newEntry;
67 }
68 }
69 }
2 using System.Collections;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.SessionState;
6 using System.IO;
7 using System.Xml.Serialization;
8
9 namespace _1asp
10 {
11 public class Global : System.Web.HttpApplication
12 {
13 //成员
14 public static String EntryFilePath;
15
16 protected void Application_Start(object sender, EventArgs e)
17 {
18 EntryFilePath = Server.MapPath("Entries");
19 }
20
21 protected void Session_Start(object sender, EventArgs e)
22 {
23
24 }
25
26 protected void Application_BeginRequest(object sender, EventArgs e)
27 {
28
29 }
30
31 protected void Application_AuthenticateRequest(object sender, EventArgs e)
32 {
33
34 }
35
36 protected void Application_Error(object sender, EventArgs e)
37 {
38
39 }
40
41 protected void Session_End(object sender, EventArgs e)
42 {
43
44 }
45
46 protected void Application_End(object sender, EventArgs e)
47 {
48
49 }
50
51 /*加载XML数据文件,并把内容放在定义的Entry类中
52 * 使用了System.Xml.Serialization名称空间中的一个类XmlSerializer,调用它的方法Deserialize()
53 * 并把它的数据转换为一组对象属性,它们存储在Entry类的实例中
54 */
55 public static Entry LoadEntry(String filename)
56 {
57 String filepath = EntryFilePath + "\\" + filename;
58 FileStream file = new FileStream(filepath, FileMode.Open);
59
60 XmlSerializer serializer = new XmlSerializer(typeof(Entry));
61 Entry newEntry = (Entry)serializer.Deserialize(file);
62
63 file.Close();
64 //显示时间
65 newEntry.Timestamp = new FileInfo(filepath).LastWriteTime;
66 return newEntry;
67 }
68 }
69 }
4.在Default.aspx文件设计图上添加2个label控件,在源码中,Page_Load事件中添加如下代码,可以观看读取的xml数据值
1 labelServerPath.Text = Server.MapPath("");
2 //load the entry from disk
3 Entry entry = Global.LoadEntry("Entry.xml");
4 labelEntryTitle.Text = entry.Title;
5 //labelEntryDetails.Text = entry.Details;
6 labelEntryDetails.Text = entry.Timestamp.ToString("dddd") + "," + entry.Timestamp.ToLongDateString() + "-" + entry.Details;
2 //load the entry from disk
3 Entry entry = Global.LoadEntry("Entry.xml");
4 labelEntryTitle.Text = entry.Title;
5 //labelEntryDetails.Text = entry.Details;
6 labelEntryDetails.Text = entry.Timestamp.ToString("dddd") + "," + entry.Timestamp.ToLongDateString() + "-" + entry.Details;
浙公网安备 33010602011771号