温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:LiveBlog v1.0测试版源码
当前文件:
LiveBlog/LiveBlog.Core/Providers/XmlProvider/Posts.cs[8K,2009-6-12 11:47:02],打开代码结构图
LiveBlog/LiveBlog.Core/Providers/XmlProvider/Posts.cs[8K,2009-6-12 11:47:02],打开代码结构图1Using 12
13
namespace LiveBlog.Core.Providers 14
{ 15
/// <summary> 16
/// A storage provider for BlogEngine that uses XML files. 17
/// <remarks> 18
/// To build another provider, you can just copy and modify 19
/// this one. Then add it to the web.config's BlogEngine section. 20
/// </remarks> 21
/// </summary> 22
public partial class XmlBlogProvider : BlogProvider 23
{ 24
//private static string _Folder = System.Web.HttpContext.Current.Server.MapPath(BlogSettings.Instance.StorageLocation); 25
26
internal static string _Folder 27
{ 28
get 29
{ 30
string p = BlogSettings.Instance.StorageLocation.Replace("~/", ""); 31
return System.IO.Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, p); 32
} 33
} 34
35
/// <summary> 36
/// Retrieves a post based on the specified Id. 37
/// </summary> 38
public override Post SelectPost(Guid id) 39
{ 40
string fileName = _Folder + "posts" + Path.DirectorySeparatorChar + id.ToString() + ".xml"; 41
Post post = new Post(); 42
XmlDocument doc = new XmlDocument(); 43
doc.Load(fileName); 44
45
post.Title = doc.SelectSingleNode("post/title").InnerText; 46
post.Description = doc.SelectSingleNode("post/description").InnerText; 47
post.Content = doc.SelectSingleNode("post/content").InnerText; 48
post.DateCreated = DateTime.Parse(doc.SelectSingleNode("post/pubDate").InnerText, CultureInfo.InvariantCulture); 49
50
if (doc.SelectSingleNode("post/lastModified") != null) 51
post.DateModified = DateTime.Parse(doc.SelectSingleNode("post/lastModified").InnerText, CultureInfo.InvariantCulture); 52
53
if (doc.SelectSingleNode("post/author") != null) 54
post.Author = doc.SelectSingleNode("post/author").InnerText; 55
56
if (doc.SelectSingleNode("post/ispublished") != null) 57
post.IsPublished = bool.Parse(doc.SelectSingleNode("post/ispublished").InnerText); 58
59
if (doc.SelectSingleNode("post/iscommentsenabled") != null) 60
post.IsCommentsEnabled = bool.Parse(doc.SelectSingleNode("post/iscommentsenabled").InnerText); 61
62
if (doc.SelectSingleNode("post/raters") != null) 63
post.Raters = int.Parse(doc.SelectSingleNode("post/raters").InnerText, CultureInfo.InvariantCulture); 64
65
if (doc.SelectSingleNode("post/rating") != null) 66
post.Rating = float.Parse(doc.SelectSingleNode("post/rating").InnerText, System.Globalization.CultureInfo.GetCultureInfo("en-gb")); 67
68
if (doc.SelectSingleNode("post/slug") != null) 69
post.Slug = doc.SelectSingleNode("post/slug").InnerText; 70
71
// Tags 72
foreach (XmlNode node in doc.SelectNodes("post/tags/tag")) 73
{ 74
if (!string.IsNullOrEmpty(node.InnerText)) 75
post.Tags.Add(node.InnerText); 76
} 77
78
// comments 79
foreach (XmlNode node in doc.SelectNodes("post/comments/comment")) 80
{ 81
Comment comment = new Comment(); 82
comment.Id = new Guid(node.Attributes["id"].InnerText); 83
comment.Author = node.SelectSingleNode("author").InnerText; 84
comment.Email = node.SelectSingleNode("email").InnerText; 85
comment.Parent = post; 86
87
if (node.SelectSingleNode("country") != null) 88
comment.Country = node.SelectSingleNode("country").InnerText; 89
90
if (node.SelectSingleNode("ip") != null) 91
comment.IP = node.SelectSingleNode("ip").InnerText; 92
93
if (node.SelectSingleNode("website") != null) 94
{ 95
Uri website; 96
if (Uri.TryCreate(node.SelectSingleNode("website").InnerText, UriKind.Absolute, out website)) 97
comment.Website = website; 98
} 99
100
if (node.Attributes["approved"] != null) 101
comment.IsApproved = bool.Parse(node.Attributes["approved"].InnerText); 102
else 103
comment.IsApproved = true; 104
105
comment.Content = node.SelectSingleNode("content").InnerText; 106
comment.DateCreated = DateTime.Parse(node.SelectSingleNode("date").InnerText, CultureInfo.InvariantCulture); 107
post.Comments.Add(comment); 108
} 109
110
post.Comments.Sort(); 111
112
// categories 113
foreach (XmlNode node in doc.SelectNodes("post/categories/category")) 114
{ 115
Guid key = new Guid(node.InnerText); 116
Category cat = Category.GetCategory(key); 117
if (cat != null)//CategoryDictionary.Instance.ContainsKey(key)) 118
post.Categories.Add(cat); 119
} 120
121
// Notification e-mails 122
foreach (XmlNode node in doc.SelectNodes("post/notifications/email")) 123
{ 124
post.NotificationEmails.Add(node.InnerText); 125
} 126
127
return post; 128
} 129
130
/// <summary> 131
/// Inserts a new Post to the data store. 132
/// </summary> 133
/// <param name="post"></param> 134
public override void InsertPost(Post post) 135
{ 136
if (!Directory.Exists(_Folder + "posts")) 137
Directory.CreateDirectory(_Folder + "posts"); 138
139
string fileName = _Folder + "posts" + Path.DirectorySeparatorChar + post.Id.ToString() + ".xml"; 140
XmlWriterSettings settings = new XmlWriterSettings(); 141
settings.Indent = true; 142
143
using (XmlWriter writer = XmlWriter.Create(fileName, settings)) 144
{ 145
writer.WriteStartDocument(true); 146
writer.WriteStartElement("post"); 147
148
writer.WriteElementString("author", post.Author); 149
writer.WriteElementString("title", post.Title); 150
writer.WriteElementString("description", post.Description); 151
writer.WriteElementString("content", post.Content); 152
writer.WriteElementString("ispublished", post.IsPublished.ToString()); 153
writer.WriteElementString("iscommentsenabled", post.IsCommentsEnabled.ToString()); 154
writer.WriteElementString("pubDate", post.DateCreated.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)); 155
writer.WriteElementString("lastModified", post.DateModified.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)); 156
writer.WriteElementString("raters", post.Raters.ToString(CultureInfo.InvariantCulture)); 157
writer.WriteElementString("rating", post.Rating.ToString(CultureInfo.InvariantCulture)); 158
writer.WriteElementString("slug", post.Slug); 159
160
// Tags 161
writer.WriteStartElement("tags"); 162
foreach (string tag in post.Tags) 163
{ 164
writer.WriteElementString("tag", tag); 165
} 166
writer.WriteEndElement(); 167
168
// comments 169
writer.WriteStartElement("comments"); 170
foreach (Comment comment in post.Comments) 171
{ 172
writer.WriteStartElement("comment"); 173
writer.WriteAttributeString("id", comment.Id.ToString()); 174
writer.WriteAttributeString("approved", comment.IsApproved.ToString()); 175
writer.WriteElementString("date", comment.DateCreated.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)); 176
writer.WriteElementString("author", comment.Author); 177
writer.WriteElementString("email", comment.Email); 178
writer.WriteElementString("country", comment.Country); 179
writer.WriteElementString("ip", comment.IP); 180
if (comment.Website != null) 181
writer.WriteElementString("website", comment.Website.ToString()); 182
writer.WriteElementString("content", comment.Content); 183
writer.WriteEndElement(); 184
} 185
writer.WriteEndElement(); 186
187
// categories 188
writer.WriteStartElement("categories"); 189
foreach (Category cat in post.Categories) 190
{ 191
//if (cat.Id = .Instance.ContainsKey(key)) 192
// writer.WriteElementString("category", key.ToString()); 193
writer.WriteElementString("category", cat.Id.ToString()); 194
195
} 196
writer.WriteEndElement(); 197
198
// Notification e-mails 199
writer.WriteStartElement("notifications"); 200
foreach (string email in post.NotificationEmails) 201
{ 202
writer.WriteElementString("email", email); 203
} 204
writer.WriteEndElement(); 205
206
writer.WriteEndElement(); 207
} 208
} 209
210
/// <summary> 211
/// Updates a Post. 212
/// </summary> 213
public override void UpdatePost(Post post) 214
{ 215
InsertPost(post); 216
} 217
218
/// <summary> 219
/// Deletes a post from the data store. 220
/// </summary> 221
public override void DeletePost(Post post) 222
{ 223
string fileName = _Folder + "posts" + Path.DirectorySeparatorChar + post.Id.ToString() + ".xml"; 224
if (File.Exists(fileName)) 225
File.Delete(fileName); 226
} 227
228
/// <summary> 229
/// Retrieves all posts from the data store 230
/// </summary> 231
/// <returns>List of Posts</returns> 232
public override List<Post> FillPosts() 233
{ 234
string folder = Category._Folder + "posts" + Path.DirectorySeparatorChar; 235
List<Post> posts = new List<Post>(); 236
237
foreach (string file in Directory.GetFiles(folder, "*.xml", SearchOption.TopDirectoryOnly)) 238
{ 239
FileInfo info = new FileInfo(file); 240
string id = info.Name.Replace(".xml", string.Empty); 241
//Post post = SelectPost(new Guid(id)); 242
Post post = Post.Load(new Guid(id)); 243
posts.Add(post); 244
} 245
246
posts.Sort(); 247
return posts; 248
} 249
250
} 251
}






