温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:中小企业网站系统前台源码(SmallBusinessStarterKit)
当前文件路径:SmallBusinessStarterKit/App_Code/News/XmlNewsProvider.cs

1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.WebControls; 8
using System.Xml; 9
using System.Xml.Schema; 10
using System.IO; 11
using System.Collections.Generic; 12
13
14
15
/// <summary> 16
/// XML Data Layer for News Page 17
/// </summary> 18
public class XmlNewsProvider : NewsProvider 19
{ 20
private string _xmlFile; 21
private string _xsdFile; 22
23
/// <summary> 24
/// Reads xml and xsd file names from the web.config file 25
/// </summary> 26
public XmlNewsProvider() 27
{ 28
SmallBusinessDataProvidersSection sec = (ConfigurationManager.GetSection("SmallBusinessDataProviders")) as SmallBusinessDataProvidersSection; 29
string xmlFile = sec.NewsProviders[sec.NewsProviderName].Parameters["dataFile"]; 30
string xsdFile = sec.NewsProviders[sec.NewsProviderName].Parameters["schemaFile"]; 31
32
_xmlFile = HttpContext.Current.Request.MapPath("~/App_Data/" + xmlFile); 33
_xsdFile = HttpContext.Current.Request.MapPath("~/App_Data/schemas/" + xsdFile); 34
} 35
36
/// <summary> 37
/// Returns all news items 38
/// </summary> 39
public override List<NewsItem> GetAllNews() 40
{ 41
DataSet dataSet = Util.ReadAndValidateXml(_xmlFile, _xsdFile); 42
List<NewsItem> list = new List<NewsItem>(); 43
foreach (DataTable t in dataSet.Tables) 44
{ 45
NewsItem curr; 46
foreach (DataRow r in t.Rows) 47
{ 48
curr = new NewsItem((string)r["id"], (bool)r["visible"], (string)r["title"]); 49
curr.Date = (r["date"] is DBNull) ? DateTime.MinValue : (DateTime)r["date"]; 50
curr.Content = (r["content"] is DBNull) ? String.Empty: (string)r["content"]; 51
curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"]; 52
curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"]; 53
list.Add(curr); 54
} 55
} 56
57
return list; 58
} 59
60
/// <summary> 61
/// Returns a particular news item 62
/// </summary> 63
public override NewsItem GetNewsItem(string newsItemId) 64
{ 65
List<NewsItem> newsItems = GetAllNews(); 66
67
foreach (NewsItem n in newsItems) 68
{ 69
if (n.Id == newsItemId) 70
return n; 71
} 72
return null; 73
} 74
75
} 76







