zoukankan      html  css  js  c++  java
  • 直接创建一个XmlDocument文档

    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>
    代码如下: 
    <%@ 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 declaration
    XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
    // Create the root element
    XmlElement rootNode  = xmlDoc.CreateElement("CategoryList");
    xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
    xmlDoc.AppendChild(rootNode);
    // Create a new <Category> element and add it to the root node
    XmlElement parentNode  = xmlDoc.CreateElement("Category");
    // Set attribute name and value!
    parentNode.SetAttribute("ID""01");
    xmlDoc.DocumentElement.PrependChild(parentNode);
    // Create the required nodes
    XmlElement mainNode  = xmlDoc.CreateElement("MainCategory");
    XmlElement descNode  = xmlDoc.CreateElement("Description");
    XmlElement activeNode  = xmlDoc.CreateElement("Active");
    // retrieve the text
    XmlText 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 value
    parentNode.AppendChild(mainNode);
    parentNode.AppendChild(descNode);
    parentNode.AppendChild(activeNode);
    // save the value of the fields into the nodes
    mainNode.AppendChild(categoryText);
    descNode.AppendChild(descText);
    activeNode.AppendChild(activeText);
    // Save to the XML file
    xmlDoc.Save( Server.MapPath("categories.xml"));
    Response.Write("XML file created");
    }
    }
    </script>
  • 相关阅读:
    linux LTIB学习笔记
    wince WaitForMultipleObjects需要注意的问题
    微信小程序在苹果上出现[request:fail 发生了 SSL 错误无法建立与该服务器的安全连接。]错误的解决方案
    Windows 2008之PKI实战4:吊销
    十个不找工作的理由
    [zt]我奋斗了18年不是为了和你一起喝咖啡
    [zt]Java/PHP/C 几种语言 RSA 的互操作
    全职共享和兼职的一些思考pkill
    定价策略(翻译稿)
    Windows 2008之PKI实战1:管理
  • 原文地址:https://www.cnblogs.com/lujin49/p/2238680.html
Copyright © 2011-2022 走看看