Linq to xml guestbook

in 未分类

随便写的,非常之简陋,也算是刚接触linq to xml的学习笔记吧

GuestBook.Core项目是数据访问、逻辑层,所有的留言的增、删、改、查都实现IDataProvider接口,本例是用的LinqXmlProvider类来实现

有些东东没写完,如管理员模式、回复留言等。

public interface IDataProvider
{
void InsertMessage(GuestBookEntry msg);
List GetData();
List GetData(int StartIndex, int PageSize);
void Reply(GuestBookEntry msg);
void DeleteMessage(string id);
}

InsertMessage的部分代码

XElement doc = XDocument.Load(DataFile).Root;

doc.Add(new XElement(“Message”,
new XElement(“ID”, msg.ID),
new XElement(“content”, new XCData(msg.Content)),
new XElement(“createdate”, msg.CreateDate.ToString(“yyyy-MM-dd HH:mm:ss”)),
new XElement(“Reply”, msg.Reply),
new XElement(“Author”,msg.Author),
new XElement(“IP”, msg.IP)));
doc.Save(DataFile);

获取

public List GetData(int StartIndex, int PageSize)
{
List List = new List();
if (File.Exists(DataFile))
{
XDocument doc = XDocument.Load(DataFile);

try
{
var list = from a in
(from c in doc.Descendants(“Message”)
select new GuestBookEntry
{
Author = c.Element(“Author”).Value,
Content = c.Element(“content”).Value,
CreateDate = DateTime.Parse(c.Element(“createdate”).Value),
ID = c.Element(“ID”).Value,
IP = c.Element(“IP”).Value,
Reply = c.Element(“Reply”).Value
})
orderby a.CreateDate descending
select a;
List = new List(list.Skip(StartIndex).Take(PageSize));
}
catch { }

}
return List;
}

删除:

public void DeleteMessage(string id)
{
XElement doc = XElement.Load(DataFile);
var a = doc.Descendants(“Message”).Single(p => p.Element(“ID”).Value == id);
a.Remove();
doc.Save(DataFile);

}

[url]http://yibin.ushttp://yibin.us/wp-content/uploads/20080216/191939_guestbook.rar[/url]

0 Comments

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>