返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

利用ASP.NET 创建、修改、删除XML程序代码

时间:2011-04-08 12:28来源:知行网www.zhixing123.cn 编辑:麦田守望者

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();
}

------分隔线----------------------------
标签(Tag):ASP.NET asp.Net实例教程 asp.net源代码 asp.net基础教程
------分隔线----------------------------
推荐内容
猜你感兴趣