.net中xml基本操作
2009-02-04 10:26
302 查看
Code
一.添加数据:
public bool AddArticle(string NewsTitle, string NewsContent, string NewsClassID)
{
XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath(articlePath)); //装载文章xml
int newID = 1;
if (doc.DocumentElement.SelectSingleNode("//Article[@ID]") != null)
{ //最后一个文章ID+1就是新的文章ID
newID = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("//Article[last()]").Attributes["ID"].Value) + 1;
}
XmlElement el = doc.CreateElement("Article");
XmlAttribute id = doc.CreateAttribute("ID");
id.Value = newID.ToString();
XmlAttribute title = doc.CreateAttribute("Title");
title.Value = NewsTitle;
XmlAttribute date = doc.CreateAttribute("Date");
date.Value = DateTime.Now.ToString();
XmlAttribute classID = doc.CreateAttribute("ClassID");
classID.Value = NewsClassID;
XmlCDataSection content = doc.CreateCDataSection(NewsContent);
el.Attributes.Append(id);
el.Attributes.Append(title);
el.Attributes.Append(classID);
el.Attributes.Append(date);
el.AppendChild(content);
doc.DocumentElement.AppendChild(el);
doc.Save(HttpContext.Current.Server.MapPath(articlePath));
return true;
}
二.修改数据
public bool EditArticle(string NewsTitle, string NewsContent, string NewsID)
{
try
{
XmlDocument document = new XmlDocument();
document.Load(HttpContext.Current.Server.MapPath(this.articlePath));
XmlNode node = document.DocumentElement.SelectSingleNode("Article[@ID=" + NewsID + "]");
if (node != null)
{
node.Attributes["Title"].Value = NewsTitle;
node.FirstChild.Value = NewsContent;
}
document.Save(HttpContext.Current.Server.MapPath(this.articlePath));
return true;
}
catch
{
return false;
}
}
三.删除数据
public bool DeleteArticle(string NewsID)
{
bool flag = false;
try
{
XmlDocument document = new XmlDocument();
document.Load(HttpContext.Current.Server.MapPath(this.articlePath));
XmlNode oldChild = document.DocumentElement.SelectSingleNode("Article[@ID=" + NewsID + "]");
if (oldChild != null)
{
oldChild.ParentNode.RemoveChild(oldChild);
}
document.Save(HttpContext.Current.Server.MapPath(this.articlePath));
}
catch
{
flag = false;
}
return flag;
}
一.添加数据:
public bool AddArticle(string NewsTitle, string NewsContent, string NewsClassID)
{
XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath(articlePath)); //装载文章xml
int newID = 1;
if (doc.DocumentElement.SelectSingleNode("//Article[@ID]") != null)
{ //最后一个文章ID+1就是新的文章ID
newID = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("//Article[last()]").Attributes["ID"].Value) + 1;
}
XmlElement el = doc.CreateElement("Article");
XmlAttribute id = doc.CreateAttribute("ID");
id.Value = newID.ToString();
XmlAttribute title = doc.CreateAttribute("Title");
title.Value = NewsTitle;
XmlAttribute date = doc.CreateAttribute("Date");
date.Value = DateTime.Now.ToString();
XmlAttribute classID = doc.CreateAttribute("ClassID");
classID.Value = NewsClassID;
XmlCDataSection content = doc.CreateCDataSection(NewsContent);
el.Attributes.Append(id);
el.Attributes.Append(title);
el.Attributes.Append(classID);
el.Attributes.Append(date);
el.AppendChild(content);
doc.DocumentElement.AppendChild(el);
doc.Save(HttpContext.Current.Server.MapPath(articlePath));
return true;
}
二.修改数据
public bool EditArticle(string NewsTitle, string NewsContent, string NewsID)
{
try
{
XmlDocument document = new XmlDocument();
document.Load(HttpContext.Current.Server.MapPath(this.articlePath));
XmlNode node = document.DocumentElement.SelectSingleNode("Article[@ID=" + NewsID + "]");
if (node != null)
{
node.Attributes["Title"].Value = NewsTitle;
node.FirstChild.Value = NewsContent;
}
document.Save(HttpContext.Current.Server.MapPath(this.articlePath));
return true;
}
catch
{
return false;
}
}
三.删除数据
public bool DeleteArticle(string NewsID)
{
bool flag = false;
try
{
XmlDocument document = new XmlDocument();
document.Load(HttpContext.Current.Server.MapPath(this.articlePath));
XmlNode oldChild = document.DocumentElement.SelectSingleNode("Article[@ID=" + NewsID + "]");
if (oldChild != null)
{
oldChild.ParentNode.RemoveChild(oldChild);
}
document.Save(HttpContext.Current.Server.MapPath(this.articlePath));
}
catch
{
flag = false;
}
return flag;
}
相关文章推荐
- 在.NET 对XML的一些基本操作
- .NET中xml的基本操作
- LinqtoXml的基本操作
- C# 实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)
- Dom4j解析XML基本操作
- XML基本操作:创建增删改
- .NET里对XML操作的方法太多!大家都给谈谈有什么区别?和各自的便利
- SQLServer XML数据的五种基本操作
- .net操作xml《三》
- xml的基本操作
- .net下操作XML的几篇文章(downmoon收集自MSDN)
- 对xml的基本读取操作
- linq to xml 基本操作
- .net 操作XML小结
- C#中XML的基本操作
- .net 操作XML小结
- 谁给我一些关于XML基本操作方面的材料?
- 编写高性能 .NET 代码 第二章:垃圾回收 基本操作
- JSP中对XML操作基本范例
- XML文件增、删、改的基本操作