Thursday 9 June 2011

XML - Create New XML Document

The time of year at looking how XML interacts with c#/vb.net is upon us and its time to start to look at the coding.

Now firstly remember to copy these Namespaces at the top of the page where you will write your code.

For c# paste in:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Data;


and vb.net paste in:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml
Imports System.Data


Now for the code for creating that new XML document from c#/vb.net code:

For c# use the following:

XmlDocument xmlDoc = new XmlDocument();
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 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);

xmlDoc.Save(Server.MapPath("categories.xml"));


and in vb.net use:

Sub create_and_save()

Dim xmlDoc As XmlDocument = New XmlDocument
Dim xmlDeclaration As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "")
Dim rootNode As XmlElement = xmlDoc.CreateElement("CategoryList")
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement)
xmlDoc.AppendChild(rootNode)

'Create a new element and add it to the root node
Dim parentNode As XmlElement = xmlDoc.CreateElement("Category")

'Set attribute name and value!
parentNode.SetAttribute("ID", "01")
xmlDoc.DocumentElement.PrependChild(parentNode)

'Create the required nodes
Dim mainNode As XmlElement = xmlDoc.CreateElement("MainCategory")
Dim descNode As XmlElement = xmlDoc.CreateElement("Description")
Dim activeNode As XmlElement = xmlDoc.CreateElement("Active")


'retrieve the text
Dim categoryText As XmlText = xmlDoc.CreateTextNode("XML")
Dim descText As XmlText = xmlDoc.CreateTextNode("This is a list my XML articles.")
Dim activeText As XmlText = 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)

xmlDoc.Save(Server.MapPath("categories.xml"))

End Sub

No comments:

Post a Comment