RssPageDemo.
是昨天那篇文章的一个简化版,还是在那个基础上面改了一下.用DataTable建了一张表,然后读取.
建一个rssTest.aspx 文件
页面的代码都删,只留这一句:
rssTest.aspx
然后在后置代码里写如下代码:
rssTest.aspx.cs
建一个rssTest.aspx 文件
页面的代码都删,只留这一句:
rssTest.aspx
Code
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="rssTest.aspx.cs" Inherits="rssTest" %>
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="rssTest.aspx.cs" Inherits="rssTest" %>
然后在后置代码里写如下代码:
rssTest.aspx.cs
Code
1 string xmlDoc = "rss.xml";
2 protected void Page_Load(object sender, EventArgs e)
3 {
4 xmlDoc = Server.MapPath(".") + xmlDoc;
5 GetRss();
6 System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
7 Response.ContentType = "text/xml";
8 doc.Load(xmlDoc);
9 doc.Save(Response.Output);
10 }
11 private void GetRss()
12 {
13 DataTable table1 = MakeDataTable();
14 System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(xmlDoc, System.Text.Encoding.UTF8);
15 writer.Formatting = System.Xml.Formatting.Indented;
16 writer.WriteStartDocument(true);
17 writer.WriteComment("RssPage");
18 writer.WriteStartElement("rss");
19 writer.WriteAttributeString("version", "2.0");
20 writer.WriteStartElement("channel");
21 writer.WriteStartElement("title");
22 writer.WriteString("Applelure");
23 writer.WriteEndElement();
24 writer.WriteStartElement("link");
25 writer.WriteString("http://" + Request.ServerVariables["SERVER_NAME"]);
26 writer.WriteEndElement();
27 writer.WriteStartElement("description");
28 writer.WriteString("Applelure");
29 writer.WriteEndElement();
30 writer.WriteStartElement("copyright");
31 writer.WriteString("Copyright 2008");
32 writer.WriteEndElement();
33 writer.WriteStartElement("language");
34 writer.WriteString("zh-cn");
35 writer.WriteEndElement();
36
37 foreach (DataRow row in table1.Rows)
38 {
39 string Id = row["id"].ToString();
40 string title = row["Title"].ToString();
41 string description = row["Description"].ToString();
42
43 writer.WriteStartElement("item");
44 writer.WriteStartElement("title");
45 writer.WriteString(title);
46 writer.WriteEndElement();
47 writer.WriteStartElement("link");
48 writer.WriteString("http://" + Request.ServerVariables["SERVER_NAME"] + "/NewsShow.aspx?ID=" + Id);
49 writer.WriteEndElement();
50 writer.WriteStartElement("description");
51 writer.WriteCData(description);
52 writer.WriteEndElement();
53 writer.WriteEndElement();
54 }
55 writer.WriteEndElement();
56 writer.WriteEndElement();
57
58
59 writer.Flush();
60 writer.Close();
61
62 }
63
64 private DataTable MakeDataTable()
65 {
66 // Use the MakeTable function below to create a new table.
67 DataTable table;
68 table = MakeNamesTable();
69
70 // Once a table has been created, use the
71 // NewRow to create a DataRow.
72 DataRow row;
73 row = table.NewRow();
74
75 // Then add the new row to the collection.
76 row["Title"] = "First";
77 row["Description"] = "AAAAA";
78 table.Rows.Add(row);
79 DataRow row1;
80 row1 = table.NewRow();
81 row1["Title"] = "aa";
82 row1["Description"] = "bb";
83 table.Rows.Add(row1);
84
85 DataRow row2 = table.NewRow();
86 row2["Title"] = "cc";
87 row2["Description"] = "dd";
88 table.Rows.Add(row2);
89
90 DataRow row3 = table.NewRow();
91 row3["Description"] = "ee";
92 table.Rows.Add(row3);
93
94 return table;
95 }
96
97 private DataTable MakeNamesTable()
98 {
99 // Create a new DataTable titled 'Names.'
100 DataTable namesTable = new DataTable("Names");
101
102 // Add three column objects to the table.
103 DataColumn idColumn = new DataColumn();
104 idColumn.DataType = System.Type.GetType("System.Int32");
105 idColumn.ColumnName = "id";
106 idColumn.AutoIncrement = true;
107 namesTable.Columns.Add(idColumn);
108
109 DataColumn titleColumn = new DataColumn();
110 titleColumn.DataType = System.Type.GetType("System.String");
111 titleColumn.ColumnName = "Title";
112 titleColumn.DefaultValue = "No Title";
113 namesTable.Columns.Add(titleColumn);
114
115 DataColumn descriptionColumn = new DataColumn();
116 descriptionColumn.DataType = System.Type.GetType("System.String");
117 descriptionColumn.ColumnName = "Description";
118 namesTable.Columns.Add(descriptionColumn);
119
120 // Create an array for DataColumn objects.
121 DataColumn[] keys = new DataColumn[1];
122 keys[0] = idColumn;
123 namesTable.PrimaryKey = keys;
124
125 // Return the new DataTable.
126 return namesTable;
127 }
128
1 string xmlDoc = "rss.xml";
2 protected void Page_Load(object sender, EventArgs e)
3 {
4 xmlDoc = Server.MapPath(".") + xmlDoc;
5 GetRss();
6 System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
7 Response.ContentType = "text/xml";
8 doc.Load(xmlDoc);
9 doc.Save(Response.Output);
10 }
11 private void GetRss()
12 {
13 DataTable table1 = MakeDataTable();
14 System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(xmlDoc, System.Text.Encoding.UTF8);
15 writer.Formatting = System.Xml.Formatting.Indented;
16 writer.WriteStartDocument(true);
17 writer.WriteComment("RssPage");
18 writer.WriteStartElement("rss");
19 writer.WriteAttributeString("version", "2.0");
20 writer.WriteStartElement("channel");
21 writer.WriteStartElement("title");
22 writer.WriteString("Applelure");
23 writer.WriteEndElement();
24 writer.WriteStartElement("link");
25 writer.WriteString("http://" + Request.ServerVariables["SERVER_NAME"]);
26 writer.WriteEndElement();
27 writer.WriteStartElement("description");
28 writer.WriteString("Applelure");
29 writer.WriteEndElement();
30 writer.WriteStartElement("copyright");
31 writer.WriteString("Copyright 2008");
32 writer.WriteEndElement();
33 writer.WriteStartElement("language");
34 writer.WriteString("zh-cn");
35 writer.WriteEndElement();
36
37 foreach (DataRow row in table1.Rows)
38 {
39 string Id = row["id"].ToString();
40 string title = row["Title"].ToString();
41 string description = row["Description"].ToString();
42
43 writer.WriteStartElement("item");
44 writer.WriteStartElement("title");
45 writer.WriteString(title);
46 writer.WriteEndElement();
47 writer.WriteStartElement("link");
48 writer.WriteString("http://" + Request.ServerVariables["SERVER_NAME"] + "/NewsShow.aspx?ID=" + Id);
49 writer.WriteEndElement();
50 writer.WriteStartElement("description");
51 writer.WriteCData(description);
52 writer.WriteEndElement();
53 writer.WriteEndElement();
54 }
55 writer.WriteEndElement();
56 writer.WriteEndElement();
57
58
59 writer.Flush();
60 writer.Close();
61
62 }
63
64 private DataTable MakeDataTable()
65 {
66 // Use the MakeTable function below to create a new table.
67 DataTable table;
68 table = MakeNamesTable();
69
70 // Once a table has been created, use the
71 // NewRow to create a DataRow.
72 DataRow row;
73 row = table.NewRow();
74
75 // Then add the new row to the collection.
76 row["Title"] = "First";
77 row["Description"] = "AAAAA";
78 table.Rows.Add(row);
79 DataRow row1;
80 row1 = table.NewRow();
81 row1["Title"] = "aa";
82 row1["Description"] = "bb";
83 table.Rows.Add(row1);
84
85 DataRow row2 = table.NewRow();
86 row2["Title"] = "cc";
87 row2["Description"] = "dd";
88 table.Rows.Add(row2);
89
90 DataRow row3 = table.NewRow();
91 row3["Description"] = "ee";
92 table.Rows.Add(row3);
93
94 return table;
95 }
96
97 private DataTable MakeNamesTable()
98 {
99 // Create a new DataTable titled 'Names.'
100 DataTable namesTable = new DataTable("Names");
101
102 // Add three column objects to the table.
103 DataColumn idColumn = new DataColumn();
104 idColumn.DataType = System.Type.GetType("System.Int32");
105 idColumn.ColumnName = "id";
106 idColumn.AutoIncrement = true;
107 namesTable.Columns.Add(idColumn);
108
109 DataColumn titleColumn = new DataColumn();
110 titleColumn.DataType = System.Type.GetType("System.String");
111 titleColumn.ColumnName = "Title";
112 titleColumn.DefaultValue = "No Title";
113 namesTable.Columns.Add(titleColumn);
114
115 DataColumn descriptionColumn = new DataColumn();
116 descriptionColumn.DataType = System.Type.GetType("System.String");
117 descriptionColumn.ColumnName = "Description";
118 namesTable.Columns.Add(descriptionColumn);
119
120 // Create an array for DataColumn objects.
121 DataColumn[] keys = new DataColumn[1];
122 keys[0] = idColumn;
123 namesTable.PrimaryKey = keys;
124
125 // Return the new DataTable.
126 return namesTable;
127 }
128