您的位置:首页 > 编程语言 > ASP

ASP.NET 创建、修改、删除XML

2008-04-08 13:35 447 查看
using System.Xml;

private void CreateXML()
{
if (System.IO.File.Exists("~/book.xml"))
{
System.IO.File.Delete("~/book.xml");
}
//创始XML对象
XmlDocument xmlDocument = new XmlDocument();
//XML声明节点
XmlDeclaration xmlDeclare = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDocument.AppendChild(xmlDeclare);

//创始根结点
XmlElement elementRoot = xmlDocument.CreateElement("BookStore");
xmlDocument.AppendChild(elementRoot);

XmlNode rootNode = xmlDocument.SelectSingleNode("BookStore");

//创建结点
XmlElement elementNode = xmlDocument.CreateElement("Book");
elementNode.SetAttribute("Genre", "IT");
elementNode.SetAttribute("ISBN", "9-858-44");

XmlElement elementTitle = xmlDocument.CreateElement("Title");
elementTitle.InnerText = "ASP.NET";
elementNode.AppendChild(elementTitle);

XmlElement elementAuthor = xmlDocument.CreateElement("Author");
elementAuthor.InnerText = "Jaem";
elementNode.AppendChild(elementAuthor);

XmlElement elementPrice = xmlDocument.CreateElement("Price");
elementPrice.InnerText = "$20.5";
elementNode.AppendChild(elementPrice);

rootNode.AppendChild(elementNode);
xmlDocument.Save(Server.MapPath("~/book.xml"));

}

private void CreateNewNode()
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(Server.MapPath("~/book.xml"));

XmlNode rootNode = xmlDocument.SelectSingleNode("BookStore");

//创建结点
XmlElement elementNode = xmlDocument.CreateElement("Book");
elementNode.SetAttribute("Genre", "Story");
elementNode.SetAttribute("ISBN", "85-748-96");

XmlElement elementTitle = xmlDocument.CreateElement("Title");
elementTitle.InnerText = "Security Password";
elementNode.AppendChild(elementTitle);

XmlElement elementAuthor = xmlDocument.CreateElement("Author");
elementAuthor.InnerText = "Stomy";
elementNode.AppendChild(elementAuthor);

XmlElement elementPrice = xmlDocument.CreateElement("Price");
elementPrice.InnerText = "$15";
elementNode.AppendChild(elementPrice);

rootNode.AppendChild(elementNode);
xmlDocument.Save(Server.MapPath("~/book.xml"));
}

private void ModifyNoed()
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(Server.MapPath("~/book.xml"));

XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode("BookStore").ChildNodes;
foreach (XmlNode XNode in xmlNodeList)
{
XmlElement xmlNode = (XmlElement)XNode;
if (xmlNode.GetAttribute("Genre") == "Modom Life")
{
xmlNode.SetAttribute("Genre", "Modom Life");
//得到子结点
XmlNodeList subNodeList = xmlNode.ChildNodes;
foreach (XmlNode subNode in subNodeList)
{
XmlElement xmlSubNode = (XmlElement)subNode;
if (xmlSubNode.Name=="Author")
{
xmlSubNode.InnerText = "Lucy And Jeny";
break;
}
}
break;
}
}
xmlDocument.Save(Server.MapPath("~/book.xml"));
}

private void DeleteAttributsAndNode()
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(Server.MapPath("~/book.xml"));

XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode("BookStore").ChildNodes;
foreach (XmlNode xmlNode in xmlNodeList)
{
XmlElement xmlElement = (XmlElement)xmlNode;
if (xmlElement.GetAttribute("Genre") == "Story")
{
//移除ISBN属性
xmlElement.RemoveAttribute("ISBN");
}
else if (xmlElement.GetAttribute("Genre") == "Train")
{
//连同属性、结点一并删除
xmlElement.RemoveAll();
}
}
xmlDocument.Save(Server.MapPath("~/book.xml"));
}

//使用

protected void Page_Load(object sender, EventArgs e)
{
CreateXML();
CreateNewNode();
ModifyNoed();
DeleteAttributsAndNode();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: