.net中xml基本操作
一.新增資料:
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;
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-545257/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- .net core Identity整合IdentityServer4 (1)基本操作IDEServer
- XML基本操作-建立(DOM和LOINQ)和LINQ查詢和儲存XML
- Spring中基於XML方式的AOP操作SpringXML
- .net持續整合sonarqube篇之sonarqube基本操作(二)
- 教程:如何在.NET中使用MongoDB以及基本的CRUD操作MongoDB
- C# 操作xml(轉)C#XML
- PHP操作xml詳解PHPXML
- PHP xml 轉陣列 陣列轉 xml 操作PHPXML陣列
- .Net 在容器中操作宿主機
- xml操作的幾種方法XML
- PHP透過DOM操作XMLPHPXML
- MongoDB基本操作MongoDB
- webpack 基本操作Web
- mongo基本操作Go
- SQL基本操作SQL
- ElasticSearch - 基本操作Elasticsearch
- candance 基本操作
- svn基本操作
- oracle基本操作Oracle
- Hash基本操作
- python基本操作Python
- FFMPEG基本操作
- dos 基本操作
- 基本操作題
- Laravel 基本操作Laravel
- Git基本操作Git
- VSCode基本操作VSCode
- Docker基本操作Docker
- redis基本操作Redis
- linux基本操作Linux
- ElasticSearch基本操作Elasticsearch
- HBase 基本操作
- Go 操作 Redis 的基本操作GoRedis
- [PowerShell] 快速入門, 基本語法, 常用型別, 函式, .NET 互操作型別函式
- .net api介面接收字串或者xmlAPI字串XML
- 關於JAVA中順序IO的基本操作Java
- 坐下坐下,基本操作(ZooKeeper 操作篇)
- Clion基本常用操作
- Redis管理基本操作Redis