Creating an XML document

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************" );

}

}

}


Comments

Author: Abul Bashar Sardar26 Sep 2013 Member Level: Gold   Points : 0

Thank you Raj Krishna for giving this code snippet which is very usefull for our project specially I'm searching such type of project.

Author: Phagu Mahato07 Oct 2013 Member Level: Gold   Points : 1

Thank it is really a useful article of beginner on the topic how to create XML document. I explain another example for this

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet dset;
string str;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}

void bind()
{

SqlConnection con = new SqlConnection(connStr);

try
{
con.Open();
str = "select * from student";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
dset = new DataSet();
sqlda.Fill(dset, "student");
GridView1.DataSource = dset;
GridView1.DataMember = "student";
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

}
protected void button_save_Click(object sender, EventArgs e)
{
bind();
dset.WriteXml("d:\\student.xml");
Response.Write("Records saved as xml");
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: