dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Home » Tip Bank » XML
Language: XML
Expertise: Beginner
May 25, 2004

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Create a New XML File Using XmlDocument

Here's the XML File:

<?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>
Here's the 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 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>
Sonu Kapoor
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date