Webservice using XML
This is a ASP.NET Webservice using C#, XML and SQL Server 2008 . which returns the countries capitals and country codes in a database. Here XMLDocument class is used and the XMLWriter and XMLReader classes also used.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;
using System.Data.SqlClient;
using System.Xml;
namespace WebApp
{
[WebService(Namespace = "http://taxipartner.in/",Description="Country capital and their codes",Name="Country Details")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
SqlConnection con = new SqlConnection("Your Connection String Name");
[WebMethod]
public XmlDocument countries()
{
string sqlQuery = "select * from country ";
con.Open();
SqlCommand com = new SqlCommand(sqlQuery, con);
SqlDataReader read = com.ExecuteReader();
XmlDocument x = new XmlDocument();
using (XmlWriter xml = XmlWriter.Create("CountryDetail.xml"))
{
xml.WriteStartDocument();
xml.WriteStartElement("Countries");
while (read.Read())
{
xml.WriteStartElement("CountryDetails");
xml.WriteElementString("Country", read["Country"].ToString());
xml.WriteElementString("Capital", read["Capital"].ToString());
xml.WriteElementString("CountryCode", read["Country_Code"].ToString());
xml.WriteEndElement();
}
xml.WriteEndElement();
xml.WriteEndDocument();
}
x.Load("CountryDetail.xml");
return x;
}
}
}