This article will show you how you can convert the dataset
into xml schema file using c# in asp.net.
Some of my previous articles are as follows: Read
XML File in Dataset And Bind To GridView In Asp.Net Using C#.Net, XML
File Generation And Data Writing Using C#.Net In Asp.Net MVC, How
To Read XML File In DataSet And Display in DataGridview Using C#.Net, Read
XML File in DataTable and Bind to DataList In Asp.Net Using C#.Net, How
To Read XML File in DataTable Using C#.Net In Asp.Net MVC, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net MVC Using
C#.Net, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net Using C#.Net,
Read
XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net.
So for this article first we will create a new asp.net application and add a folder for saving the created xml file.
So for this article first we will create a new asp.net application and add a folder for saving the created xml file.
Now we will add a page add the below code into the page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
namespace WebApplication2
{
public partial class WebForm20 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
DataSet
objDataSet = new DataSet();
objDataSet = GetDataInDataSet();
StreamWriter
objXMLStreamWriter = new StreamWriter(Server.MapPath(@"\XMLDataFile\StidentXML.xml"));
objDataSet.WriteXml(objXMLStreamWriter, XmlWriteMode.WriteSchema);
objXMLStreamWriter.Close();
}
/// <summary>
/// Function to prepate data of dataset
/// </summary>
/// <returns></returns>
protected
DataSet GetDataInDataSet()
{
DataSet
objDataSet = new DataSet();
DataTable
objDataTable = new DataTable("StudentTable");
objDataTable.Columns.Add("Id", typeof(int));
objDataTable.Columns.Add("Name", typeof(string));
objDataTable.Columns.Add("Address", typeof(string));
DataRow
objDataRow1 = objDataTable.NewRow();
objDataRow1["Id"] = 1;
objDataRow1["Name"] = "Name
1";
objDataRow1["Address"] = "Address
1";
objDataTable.Rows.Add(objDataRow1);
DataRow
objDataRow2 = objDataTable.NewRow();
objDataRow2["Id"] = 2;
objDataRow2["Name"] = "Name
2";
objDataRow2["Address"] = "Address
2";
objDataTable.Rows.Add(objDataRow2);
objDataSet.Tables.Add(objDataTable);
return
objDataSet;
}
}
}
|
In this code I have created a dataset with some data you can pass your dataset. Now I have used stream writer thee write create the xml file at given location. As we know dataset have a WriteXml function this function is used for converting the dataset into the xml file.
Now we have done run the application to check the output.
Here is the created xml file code.
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="StudentTable">
<xs:complexType>
<xs:sequence>
<xs:element name="Id" type="xs:int" minOccurs="0" />
<xs:element name="Name"
type="xs:string" minOccurs="0" />
<xs:element name="Address" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<StudentTable>
<Id>1</Id>
<Name>Name 1</Name>
<Address>Address 1</Address>
</StudentTable>
<StudentTable>
<Id>2</Id>
<Name>Name 2</Name>
<Address>Address 2</Address>
</StudentTable>
</NewDataSet>
|
Please let me know your comment.
0 comments:
Please let me know your view