Tuesday, March 25, 2008

.Net Working with Xml

Xml is a World Wide Web Consortium Initiative for creating custom tags. It is based on text file.
Xml can be used as multiplatform data exchange format.


Types of XML Documents

1. Tag Based




In this Xml Document I have defined tags like Shalvin and Specialization.
Tag based xml is a better format than attribute based, but takes up more space.

2. Attribute based XML





Here ID and CategoryName are defined as attributes to Category.
3. Hybrid Xml










In a hybrid xml you will see elements of both tag and attribute. Hybrid xml is well suited for representing hierarchical data.


ReadXml, WriteXml and WriteXmlSchema methods



In the following example we are reading the contents of an XML document into a dataset and showing the contents in a DataGrid.
The user can enter new values into the DataGrid and on pressing the Save button the new data will be persisited back into the previous XML document that mimicing the data entry scenario.
DataSet.WriteXml method can be used to extract the Schema (structure) of an XML docuement.


DataSet ds = new DataSet();

private void Form1_Load(object sender, EventArgs e)
{
ds.ReadXml("c:\\Framework.xml");
dataGrid1.DataSource = ds;
}

private void btnSave_Click(object sender, EventArgs e)
{
ds.WriteXml("c:\\Framework.xml");
MessageBox.Show("Xml saved successfully");
}

private void btnWriteXmlSchema_Click(object sender, EventArgs e)
{
ds.WriteXmlSchema("c:\\FrameworkSchema.xml");
}

DataAdapter DataSet DataSet.WriteXml Method

WriteXml
method can be used to write the Data of an XML Document to a Xml File.

using System.Data.SqlClient;
SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
cnn = new SqlConnection("Integrated security=sspi;Initial Catalog=NorthWind");
cnn.Open();

da = new SqlDataAdapter("SELECT CategoryId,categoryName,Description from Categories", cnn);
da.Fill(ds,"cat");
da = new SqlDataAdapter("SELECT ProductId,ProductName,UnitPrice from Products", cnn);

da.Fill(ds, "Prod");
}
private void btnWriteXml_Click(object sender, EventArgs e)
{
ds.WriteXml(@"e:\\Categories.xml");
}


XmlDocument Load Methods


XmlDocument is an in memory tree representation of an XML.


using System.Xml;

XmlDocument xd = new XmlDocument();

private void Form1_Load(object sender, EventArgs e)
{
XmlDocument xd = new XmlDocument();
xd.Load(@"c:\\Framework.xml");
textBox1.Text = xd.InnerText;
}

XmlTextWriter

XmlTextWriter
is a writer that produce streams of file containing xml data.

using System.Xml;
XmlTextWriter xtw;
protected void Page_Load(object sender, EventArgs e)
{
xtw = new XmlTextWriter("c:\\Contacts.xml", null);
xtw.WriteStartElement("Contacts");
xtw.WriteStartElement("Contact");
xtw.WriteStartElement("Name");
xtw.WriteString("Shalvin");
xtw.WriteEndElement();
xtw.WriteStartElement("Location");
xtw.WriteString("Kochi");
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.Close();
}

XmlTextReader

In this example we will be reading the content of an xml file and populate the Title of form accordingly.









using System.Xml;

XmlTextReader xmlr = new XmlTextReader("f:\\Settings.xml");

private void Form1_Load(object sender, EventArgs e)
{
while(xmlr.Read())
switch (xmlr.Site)
{
case "Name":
this.Text = xmlr.ReadString();
break;
}


Xslt
Xslt is to XML what CSS is to Html. Xslt can be used to give a good look and feel for xml.



















Extracting XML from SQL Server tables
It is possible to extract the contents of a Sql SELECT as XML.

for xml auto
The following SQL statements will produce an attribute based xml document.

select CategoryId, CategoryName, description from Categories for xml auto

for xml auto, elements
The following SQL statements will produce an attribute tag xml document.

select CategoryId, CategoryName, description from Categories for xml auto, elements

Related Blog
DataTable DataColumn and DataRow (Asp.Net)

Linq to Xml (Code Snippets)

No comments:

Post a Comment