DotNet Academy of Rajesh Rolen

Solutions by Rajesh Rolen

Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Use HostelsClub Search Web-Service in ASP.NET

HostelsClub is one of the worlds larges search engine to search hotels, hostels, rooms and also provide booking of it. so if you want to integrate such a functionality in your website then you will have to take authentication from hostelclub and he will provide you Authentication id and password for Test (free of cost) and later you can upgrade to "Production" version (it will take some percentage in booking cost's profit).

HostelsClub provides you API Library and sample application in PHP to get connect with hostelsclub. But they don't have any API Library or application in ASP.NET. so if you want to do this, below sample can help you.

Through this sample you can PING the hostelsclub web-service search engine.

string targetUri = "http://www.hostelspoint.com/xml/xml.php";
System.Xml.XmlDocument reqDoc = new System.Xml.XmlDocument();
reqDoc.Load(Server.MapPath("~\\ping.xml"));//this is your xml file which u want to send to hostelsclub for ping
string formParameterName = "OTA_request";
string xmlData = reqDoc.InnerXml;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri);
string sendString = formParameterName + "=" + HttpUtility.UrlEncode(xmlData);
//string sendString = HttpUtility.UrlEncode(xmlData);

byte[] byteStream;
byteStream = System.Text.Encoding.UTF8.GetBytes(sendString);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteStream.LongLength;

using (Stream writer = request.GetRequestStream())
{
writer.Write(byteStream, 0, (int)request.ContentLength);
writer.Flush();
}

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
string respStr = "";
if (request.HaveResponse)
{
if (resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.Accepted)
{
StreamReader respReader = new StreamReader(resp.GetResponseStream());
respStr = respReader.ReadToEnd(); // get the xml result in the string object

XmlDocument doc = new XmlDocument();
doc.LoadXml(respStr);
Label1.Text = doc.InnerXml.ToString();


}
}

//===================PING.XML=======================
< ?xml version="1.0" encoding="UTF-8"? >
< OTA_PingRQ xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05OTA_PingRQ.xsd" TimeStamp="2009-12-28T11:43:43-05:00" Target="Test" Version="1.006" PrimaryLangID="en" EchoToken="1" >
< EchoData >Hello World< /EchoData >
< /OTA_PingRQ >

How to send/recive XML file through HTTP Post

If you want to send a XML file through HTTP using POST to any web service which may /may not build in .net. this example can help u:
string targetUri = "http://www.targeUrl.com";
System.Xml.XmlDocument reqDoc = new System.Xml.XmlDocument();
reqDoc.Load(Server.MapPath("~\\myfile.xml"));//xml file which you want to send
string formParameterName = "OTA_request";//request type
string xmlData = reqDoc.InnerXml;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri);
string sendString = formParameterName + "=" + HttpUtility.UrlEncode(xmlData);
//string sendString = HttpUtility.UrlEncode(xmlData);

byte[] byteStream;
byteStream = System.Text.Encoding.UTF8.GetBytes(sendString);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteStream.LongLength;

using (Stream writer = request.GetRequestStream())
{
writer.Write(byteStream, 0, (int)request.ContentLength);
writer.Flush();
}

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
string respStr = "";
if (request.HaveResponse)
{
if (resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.Accepted)
{
StreamReader respReader = new StreamReader(resp.GetResponseStream());
respStr = respReader.ReadToEnd(); // get the xml result in the string object

XmlDocument doc = new XmlDocument();
doc.LoadXml(respStr);
Label1.Text = doc.InnerXml.ToString();


}
}

How to send/recive XML file through HTTP Post

If you want to send a XML file through HTTP using POST to any web service which may /may not build in .net. this example can help u:
string targetUri = "http://www.targeUrl.com";
System.Xml.XmlDocument reqDoc = new System.Xml.XmlDocument();
reqDoc.Load(Server.MapPath("~\\myfile.xml"));//xml file which you want to send
string formParameterName = "OTA_request";//request type
string xmlData = reqDoc.InnerXml;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri);
string sendString = formParameterName + "=" + HttpUtility.UrlEncode(xmlData);
//string sendString = HttpUtility.UrlEncode(xmlData);

byte[] byteStream;
byteStream = System.Text.Encoding.UTF8.GetBytes(sendString);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteStream.LongLength;

using (Stream writer = request.GetRequestStream())
{
writer.Write(byteStream, 0, (int)request.ContentLength);
writer.Flush();
}

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
string respStr = "";
if (request.HaveResponse)
{
if (resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.Accepted)
{
StreamReader respReader = new StreamReader(resp.GetResponseStream());
respStr = respReader.ReadToEnd(); // get the xml result in the string object

XmlDocument doc = new XmlDocument();
doc.LoadXml(respStr);
Label1.Text = doc.InnerXml.ToString();


}
}

Data into DataGridView from XML file

In this article we are going to know how to get data in DataGridView from XML file.
now we create an XML file.

Create an XML file:

< ?xml version="1.0" encoding="utf-8" ? >
< bookstore >
< book >
< title > The < /title >
< price > 8.99 < /price >
< genre > autobiograph < /genre >
< publicationdate > 1981 < /publicationdate >
< ISBN > 1-861003-11- < /ISBN >
< PageSize > 500 < /PageSize >
< /book >

< book >
< title > The Confiden < /title >
< price > 11.99 < /price >
< genre > novel < /genre >
< publicationdate > 1967 < /publicationdate >
< ISBN > 0-201-63361- < /ISBN >
< /book >

< book >
< title > The Gorgias < /title >
< price > 9.99 < /price >
< genre > philosophy < /genre >
< publicationdate > 1991 < /publicationdate >
< ISBN > ss1-861001-57- < /ISBN >
< PageSize > 600 < /PageSize >
< /book >
< /bookstore >

Create the window application:
There are the following steps for creating the window application.

Step 1: Create new project.

Step 2: Drag and drop DataGridView control from the toolbox on form, and fill all required properties.

Step 3: Add the following namespace to page (form):

using System.Data.SqlClient;
using System.Xml;

Step 4: Write the code on the form load event.

For Example:

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml("C:\\books.xml");
DataSet ds = new DataSet("Books DataSet");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "Book";
}
}
}

Data into DataGridView from XML file

In this article we are going to know how to get data in DataGridView from XML file.
now we create an XML file.

Create an XML file:

< ?xml version="1.0" encoding="utf-8" ? >
< bookstore >
< book >
< title > The < /title >
< price > 8.99 < /price >
< genre > autobiograph < /genre >
< publicationdate > 1981 < /publicationdate >
< ISBN > 1-861003-11- < /ISBN >
< PageSize > 500 < /PageSize >
< /book >

< book >
< title > The Confiden < /title >
< price > 11.99 < /price >
< genre > novel < /genre >
< publicationdate > 1967 < /publicationdate >
< ISBN > 0-201-63361- < /ISBN >
< /book >

< book >
< title > The Gorgias < /title >
< price > 9.99 < /price >
< genre > philosophy < /genre >
< publicationdate > 1991 < /publicationdate >
< ISBN > ss1-861001-57- < /ISBN >
< PageSize > 600 < /PageSize >
< /book >
< /bookstore >

Create the window application:
There are the following steps for creating the window application.

Step 1: Create new project.

Step 2: Drag and drop DataGridView control from the toolbox on form, and fill all required properties.

Step 3: Add the following namespace to page (form):

using System.Data.SqlClient;
using System.Xml;

Step 4: Write the code on the form load event.

For Example:

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml("C:\\books.xml");
DataSet ds = new DataSet("Books DataSet");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "Book";
}
}
}

Export Data Grid Content to an XML File

we can use the RenderControl method to extract the content into an .xml file from datagridview.
your Data Grid is not associated with any dataset.and not using any XML support functions

The following code explains:


ArrayList al = new ArrayList();
for(int i=0;i<=10;i++)
{
al.Add(i+"");
}
DataGrid1.DataSource=al;
DataGrid1.DataBind();
Response.Clear();
Response.AddHeader("content-disposition","fileattachment;filename=xmltest.xml");
Response.ContentType="text/xml";

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DataGrid1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();

Export Data Grid Content to an XML File

we can use the RenderControl method to extract the content into an .xml file from datagridview.
your Data Grid is not associated with any dataset.and not using any XML support functions

The following code explains:


ArrayList al = new ArrayList();
for(int i=0;i<=10;i++)
{
al.Add(i+"");
}
DataGrid1.DataSource=al;
DataGrid1.DataBind();
Response.Clear();
Response.AddHeader("content-disposition","fileattachment;filename=xmltest.xml");
Response.ContentType="text/xml";

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DataGrid1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();

About this blog

My Blog List

Advertise On This Site

Site Info

Advertise on this Site

To advertise on this site please mail on RajeshRolen@gmail.com

Information Source

About

Pages

Dot Net Academy

Advertis in This Area of Site

Powered by Blogger.

Followers

Search This Blog