ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

ADO.NET and XML
Previous Home Next

XML stands for EXtensible Markup Language.XML was designed to carry data, not to display data and XML tags are not predefined,we must define your own tags.XML is designed to be self-descriptive and XML is a W3C Recommendation.XML is a markup language that can be used to represent complicated data in a hierarchical format.The format of the generated XML document is far more readable than the ADO equivalent – the columns are represented by elements, not attributes, and there aren't a lot of unnecessary XML namespaces:

XML Parser

An XML parser is a program that extracts the data and data descriptions and reads XML document from the XML data And it enables to programmatically work with an XML document without having to manually parse the file.

The format of the generated XML document is far more readable than the ADO equivalent – the columns are represented by elements, not attributes, and there aren't a lot of unnecessary XML namespaces:

A Example of XML Document
<?xml version="1.0">
<customer type="web">
<firstName>Kamal</firstName>
<lastName>Mishra</lastName>
<address>Ashok nagar Delhi </address>
<city>New Delhi</city>
<zip>101100</zip>
</customer>

We can load any well-formed XML document into a DataSet, without having to use a predefined structure (although we might lose content if the structure of the document is not basically tabular).

// Store the XML document in a string
string xmlDoc = @"<?xml version='1.0'?>
<books>
<book>
<title>xml and ado .net</title>
<publisher>R4R</publisher>
</book>
</books>";
// Load this into a StringReader
StringReader sr = new StringReader(xmlDoc);
// Create a new DataSet and read in the XML
DataSet ds = new DataSet();
ds.ReadXml(sr);
// Display the column names and row data as a table
foreach (DataColumn dc in ds.Tables[0].Columns)
{
Console.Write("{0,-15}", dc.ColumnName);
}
Console.Write("\n");
foreach (DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine("{0,-15}{1,-15}", dr[0], dr[1]);
}
Creating a DataSet from an XML File

The DataSet object has an overloaded method named ReadXml() that enables you to read XML data from a string, stream, or file.

dataSet.ReadXml("file.xml");

 

ReadXml()Method

ReadXml() is used to perform the magic of locating and loading the XML file. Readxml() is a very robust method, and used with different way depending on what objects we pass ReadXml(), it will perform different actions:

  • ReadXml( XmlReader reader, XmlReadMode mode )— When we are passing an XmlReader as the first argument, ReadXml() will read data from an XmlReader object.
  • ReadXml( Stream stream, XmlReadMode Mode )—When we are passing a stream as the first argument, ReadXml() will read data from a stream.
  • ReadXml( String fileName, XmlReadMode Mode )— When we are passing a filename to ReadXml(), it will retrieve XML from a file.

XmlReadMode Options for Retrieving XML

Code Symbol
Auto ReadXml() automatically select XmlReadMode by examining the XML document.
ReadSchema ReadXml() reads XML schema and loads both data and schema into DataSet
IgnoreSchema ReadXml()completely ignores XML schema and try to load data into DataSet using existing DataSet schema.
InferSchema ReadXml()ignores explicit schema information in the XML using the structure of the XML data as the schema.
DiffGram ReadXml()will read the XML as a DiffGram, appending and merging rows as needed.
Fragment ReadXml()will read the document as partial XML and import the data matching the DataSet schema and ignore the rest.

Serialization

Serialization is defined as a way of saving the state of an object. In Microsoft .NET, serialization refers to the conversion of an object's values into XML. Deserialization normally refers to the reverse process of building an object based on saved values in an XML document.

Viewing the Contents of a DataSet

The GetXml() method of the DataSet object is used that return the DataSet's contents in XML form as a string. First, a table of information is retrieved from the table in database. Then it is placed into a label Web control in a Web form in order to display it.

EXAMPLE:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<HTML>
<HEAD>
<script language="c#" runat="server" >
void  Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=compee;Initial 
Catalog=Emp;Integrated Security=sspi"); 
SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn)
SqlDataAdapter adapt= new SqlDataAdapter(cmd)
DataSet dsCustomers = new DataSet()
conn.Open()
adapt.Fill(dsCustomers, "Customers")
conn.Close()
lblOutput.Text = dsCustomers.GetXml()
}
</script>
</HEAD>
<body>
<form runat="server">
XML Output:<br>
<asp:label id="lblOutput" runat="server"/>
</form>
</body>
</html>

Writing a DataSet to an XML File

WriteXml().method is used to writing a dataset to an xml file.

There are three different XmlWriteModes:

Code Description
IgnoreSchema Writes the DataSet as XML without any additional schema information.
WriteSchema Writes the DataSet as XML with additional schema information included as inline XSD schema.
DiffGram Writes the DataSet as a DiffGram.

DiffGram : A DiffGram is an XML document that is used to store original and current values for the data in a DataSet.

EXAMPLE

void  Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection
("Data Source=compee;Initial 
Catalog=Emp;Integrated Security=sspi"); 
SqlCommand cmd = new SqlCommand
("SELECT * FROM Customers", conn)
SqlDataAdapter adapt= new SqlDataAdapter(cmd)
DataSet dsCustomers = new DataSet()
conn.Open()
adapt.Fill(dsCustomers, "Customers")
conn.Close()
dsCustomers.WriteXml(Server.MapPath
("XMLFiles/MyXMLFile.xml"));
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback:
>