May 23, 2012

Create XML File in C#.NET using XmlTextWriter


the code sample is a function CreateXmlFile that creates a Xml file with a name as specified in it's argument.

The code operates based on a XmlTextWriter object that has methods to create an XML file, specify it's encoding, start and end elements in the xml files, etc.
Some functions are used in the example.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Xml_ex
{
    public partial class WriteXml : Form
    {
       
        public WriteXml()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlTextWriter writer = new XmlTextWriter("Config.xml", System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            writer.WriteStartElement("Configuration");
            createNode("Yes","Yes","A","B","c","d","e","f","g", writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MessageBox.Show("XML File created ! ");

        }

        private void createNode(string usrDomain, string usrWorkgrp,string usrName,string password,
string serverShare,string reportShare,string schDay,string schDate,string schTime,XmlTextWriter writer)
        {
            writer.WriteStartElement("Memberof");
            writer.WriteStartElement("Domain");
            writer.WriteString(usrDomain);
            writer.WriteEndElement();
            writer.WriteStartElement("Domain");
            writer.WriteStartAttribute("Isdomain");
            writer.WriteString("Yes");

             writer.WriteEndAttribute();
             writer.WriteEndElement();
            writer.WriteStartElement("WorkGroup");
            writer.WriteString(usrWorkgrp);
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteStartElement("LogonInfo");
            writer.WriteStartElement("Username");
            writer.WriteString(usrName);
            writer.WriteEndElement();
            writer.WriteStartElement("Password");
            writer.WriteString(password);
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteStartElement("ShareFolders");
            writer.WriteStartElement("ServerShare");
            writer.WriteString(serverShare);
            writer.WriteEndElement();
            writer.WriteStartElement("ReportShare");
            writer.WriteString(reportShare);
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteStartElement("ScheduledInfo");
            writer.WriteStartElement("Day");
            writer.WriteString(schDay);
            writer.WriteEndElement();
            writer.WriteStartElement("Date");
            writer.WriteString(schDate);
            writer.WriteEndElement();
            writer.WriteStartElement("Time");
            writer.WriteString(schTime);
            writer.WriteEndElement();
            writer.WriteEndElement();


           
        }
    }
}






XML file will be like


<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
- <Configuration>
- <Memberof>
  <Domain>Yes</Domain>
  <Domain Isdomain="Yes" />
  <WorkGroup>Yes</WorkGroup>
  </Memberof>
- <LogonInfo>
  <Username>A</Username>
  <Password>B</Password>
  </LogonInfo>
- <ShareFolders>
  <ServerShare>c</ServerShare>
  <ReportShare>d</ReportShare>
  </ShareFolders>
- <ScheduledInfo>
  <Day>e</Day>
  <Date>f</Date>
  <Time>g</Time>
  </ScheduledInfo>
  </Configuration>

No comments:

Post a Comment