用xmldocument创建一个Xml文件
2008-11-20 14:10
489 查看
XML 文件:
<?xml version="1.0" encoding="utf-8"?> <CategoryList> <Category ID="01"> <MainCategory>XML</MainCategory> <Description>This is a list my XML articles.</Description> <Active>true</Active> </Category> </CategoryList>[/code]代码如下:
<%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Xml" %><%@ Page Language="C#" Debug="true" %><script runat="server">void Page_Load(object sender, System.EventArgs e){if(!Page.IsPostBack){XmlDocument xmlDoc = new XmlDocument();// Write down the XML declarationXmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);// Create the root elementXmlElement rootNode = xmlDoc.CreateElement("CategoryList");xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);xmlDoc.AppendChild(rootNode);// Create a new <Category> element and add it to the root nodeXmlElement parentNode = xmlDoc.CreateElement("Category");// Set attribute name and value!parentNode.SetAttribute("ID", "01");xmlDoc.DocumentElement.PrependChild(parentNode);// Create the required nodesXmlElement mainNode = xmlDoc.CreateElement("MainCategory");XmlElement descNode = xmlDoc.CreateElement("Description");XmlElement activeNode = xmlDoc.CreateElement("Active");// retrieve the textXmlText categoryText= xmlDoc.CreateTextNode("XML");XmlText descText = xmlDoc.CreateTextNode("This is a list my XML articles.");XmlText activeText = xmlDoc.CreateTextNode("true");// append the nodes to the parentNode without the valueparentNode.AppendChild(mainNode);parentNode.AppendChild(descNode);parentNode.AppendChild(activeNode);// save the value of the fields into the nodesmainNode.AppendChild(categoryText);descNode.AppendChild(descText);activeNode.AppendChild(activeText);// Save to the XML filexmlDoc.Save( Server.MapPath("categories.xml"));Response.Write("XML file created");}}</script>
相关文章推荐
- 用xmldocument创建一个Xml文件
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
- asp输出xml格式文件时的一个诡异错误及解决方法:XML declaration allowed only at the start of the document
- PHP 通过DOM方式创建一个book.xml文件
- 用JS创建一个XML文件
- C# 如何创建一个xml文件?并写入指定内容?
- 创建一个xml文件(c:/test.html),为该文件增加三个节点,,,将b的attribute改为4并保存
- 一个创建XMLHttpRequest对象的js文件(适用asp,php,jsp,asp.net等)
- GTK+-3.0编程 (五) GtkBuilder用于快速创建界面,它读取一个xml文件
- 如何在Android Studio中创建一个selector.xml文件
- 用JS创建一个XML文件
- 用JS创建一个XML文件
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- PHP 通过DOM方式创建一个book.xml文件
- 一个经过改良的XMLHelper(包含了序列化,反序列化,创建xml文件,读取节点,C#对象与xml文件的相互转换等等。)
- 创建一个XML文件
- C# 如何创建一个xml文件?并写入指定内容?
- PHP DOM创建生成一个XML文件
- DOM4J合并多个xml文件到一个Document
- 使用dom4j创建一个xml文件