The .NET gives us flexibility to work with the XML files easily. .NET provides following five namespaces for XML manipulation.



  1. System.Xml:- Contains major XML classes.This namespace contains many classes to read and write XML documents.Some classes under this namespace are XmlReader, XmlTextReader, XmlValidatingReader, XmlNodeReader, XmlWriter, and XmlTextWriter.

  2. System.Xml.Schema:- Contains classes which work with XML schemas. XmlSchema, XmlSchemaAll, XmlSchemaXPath, and XmlSchemaType are the classes comes under this namespace.. 

  3. System.Xml.Serialization:- Contains classes that are used to serialize objects into XML format documents or streams.

  4. System.Xml.XPath:- Contains XPath related classes to use XPath specifications. XPathDocument, XPathExression, XPathNavigator, and XPathNodeIterator classes comes under this namespace..

  5. System.Xml.Xsl :- Contains classes to work with XSLT transformations.


 


Example: Demonstrate Creation of XML Document


 


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 _CSharpApplication


{


  public partial class Form10 : Form


    {


    public Form10()


        {


    InitializeComponent();


        }


 


    private void button1_Click( object sender, EventArgs e)


        {


        XmlDocument xmldoc;


        XmlNode xmlnode;


        XmlElement xmlelem;


        XmlElement xmlelem2;


        XmlElement xmlelem3;


        XmlText xmltext;


 


        xmldoc= new XmlDocument ();


    //XML DECLARATION SECTION


        xmlnode=xmldoc.CreateNode( XmlNodeType .XmlDeclaration, "" , "" );


        xmldoc.AppendChild(xmlnode);


    //STARTING NODE


        xmlelem=xmldoc.CreateElement( "" ,txtRoot.Text, "" );


        xmltext=xmldoc.CreateTextNode(txtRootDesc.Text);


        xmlelem.AppendChild(xmltext);


        xmldoc.AppendChild(xmlelem);


    //ADD CHILD ELEMENT TO THIS NODE


        xmlelem2=xmldoc.CreateElement( "" ,txtFirstChild.Text, "" );


        xmltext=xmldoc.CreateTextNode(txtFirstChildDesc.Text);


        xmlelem2.AppendChild(xmltext);


        xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);


    //ADD CHILD ELEMENT TO THIS NODE


        xmlelem3 = xmldoc.CreateElement( "" ,txtSecondChild.Text, "" );


        xmltext = xmldoc.CreateTextNode(txtSecondChildDesc.Text);


        xmlelem3.AppendChild(xmltext);


        xmldoc.ChildNodes.Item(1).AppendChild(xmlelem3);


        //SAVE THE XML DOCUMENT IN A FILE "C:\CREATEXML.XML"


    try {


            xmldoc.Save( "c:\\CREATEXMLDocument.xml" );


        } catch ( Exception ex) {


        Console.WriteLine(ex.Source);


        }


    MessageBox.Show( "***********XML DOCUMENT CREATED************" );


    }


 }


}


 


 


Output:


 


 




 


Clicking on the “Create XML Document” after filling all the fields.


 




 


 


 


Generated XML files looks like:


 


<?xml version="1.0" ?>


<RootName>


  <FirstChildName> FirstChildDesc </FirstChildName>


  <SecondChildName> SecondChildDesc </SecondChildName>


</RootName>


 

                    

Copyright © 2010 VisualBuilder. All rights reserved