Thursday 30 August 2012

Serialize/Deserialize XML from Class


Class File :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace WindowsFormsApplication2
{
    [Serializable]
    [XmlRoot(ElementName = "Person")]
    public class clsRoot
    {
        public clsRoot() { Address = new Address(); }

        [XmlElement("Name")]
        public string Name { getset; }

        [XmlElement("Job")]
        public string Job { getset; }

        public Address Address { getset; }

    }


    [Serializable]
    [XmlRoot(ElementName = "Address")]
    public class Address
    {
        public Address() { }       
        public Permanant Permanant { getset; }
        public Current Current { getset; }
    }


    [Serializable]
    [XmlRoot(ElementName = "Permanant")]
    public class Permanant
    {
        public Permanant() { }
        [XmlAttribute("Type")]
        public string Type { getset; }
        public string Address { getset; }
        public string City { getset; }
        public string State { getset; }
        public string PinCode { getset; }
    }


    [Serializable]
    [XmlRoot(ElementName = "Current")]
    public class Current
    {
        public Current() { }
        [XmlAttribute("Type")]
        public string Type { getset; }
        public string Address { getset; }
        public string City { getset; }
        public string State { getset; }
        public string PinCode { getset; }
    }

}


Form with Read/Write button
  private void Generate_Click(object sender, EventArgs e)
        {
            clsRoot clsRoot = new clsRoot();
            clsRoot.Name = "Ajay Patel";
            clsRoot.Job = "Software Developer";

            Address ADDr = new Address();
            Permanant PR = new Permanant();
            Current Cur = new Current();

            PR.Type = "Permanant";

            PR.Address = "Panchvati";
            PR.City = "Kalol";
            PR.State = "Gujarat";
            PR.PinCode = "382722";

            Cur.Type = "Current";
            Cur.Address = "Panchvati";
            Cur.City = "Kalol";
            Cur.State = "Gujarat";
            Cur.PinCode = "382722";

            ADDr.Permanant = PR;
            ADDr.Current = Cur;

            clsRoot.Address = ADDr;

            System.Xml.Serialization.XmlSerializer xmlSerl = new
               System.Xml.Serialization.XmlSerializer(clsRoot.GetType());
            StreamWriter SW = new StreamWriter(txtXML.Text);
            xmlSerl.Serialize(SW, clsRoot);
            SW.Close();


        }
        private void txtXML_MouseDoubleClick(object sender,
                                                            MouseEventArgs e)
        {
            FileDilog.ShowDialog();
            txtXML.Text = FileDilog.FileName;
        }

        private void txtRead_Click(object sender, EventArgs e)
        {
            clsRoot clsRoot = new clsRoot();
            XmlSerializer xmlSerl = new XmlSerializer(clsRoot.GetType());
            TextReader objTxtRead = new StreamReader(txtXML.Text);
            clsRoot = (clsRoot)xmlSerl.Deserialize(objTxtRead);
            objTxtRead.Close();
        }

XML :
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>Ajay Patel</Name>
  <Job>Software Developer</Job>
  <Address>
    <Permanant Type="Permanant">
      <Address>Panchvati</Address>
      <City>Kalol</City>
      <State>Gujarat</State>
      <PinCode>382722</PinCode>
    </Permanant>
    <Current Type="Current">
      <Address>Panchvati</Address>
      <City>Kalol</City>
      <State>Gujarat</State>
      <PinCode>382722</PinCode>
    </Current>
  </Address>
</Person>

Monday 6 August 2012

Currency Conversion using webservice


Add web referance from given URL. It's Free


Then write following code in Code behind :

using SampleCSharpApplication.net.webservicex.www;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Currency cur = new Currency();
Array arr;
arr = System.Enum.GetValues(cur.GetType());
ddlFrom.DataSource = arr;
ddlFrom.DataBind();
ddlTo.DataSource = arr;
ddlTo.DataBind();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
CurrencyConvertor CC = new CurrencyConvertor();
Currency cur = new Currency();
txtResult.Text = CC.ConversionRate((Currency)System.Enum.Parse(cur.GetType(),
ddlFrom.Text), (
Currency)System.Enum.Parse(cur.GetType(),
ddlTo.Text)).ToString();
}


<div>
<asp:Literal ID="ltrfrom" runat="server"
Text="Select Currency to convert :"></asp:Literal>
<asp:DropDownList ID="ddlFrom" runat="server"
Style="vertical-align: middle;">
</asp:DropDownList>
<br />
<asp:Literal ID="ltrTo" runat="server"
Text="Select Currency convert to :"></asp:Literal>
<asp:DropDownList ID="ddlTo" runat="server"
Style="vertical-align: middle;">
</asp:DropDownList>
<br />
</div>
<asp:Button ID="Button1" runat="server"
Text="Button" onclick="Button1_Click" />
<asp:TextBox ID="txtResult" runat="server" ></asp:TextBox>

Currency Conversion Using JavaScript

<script type="text/javascript">
function convert() {
var fromCur = document.getElementById('txtVal').value +
document.getElementById(
'ddlFrom').value;
var ToCur = document.getElementById('ddlTo').value;
var strURL = 'http://www.google.com/ig/calculator?hl=en&q=' +
fromCur + '=?' + ToCur
//Url like this " http://www.google.com/ig/calculator?hl=en&q=1USD=?EUR"

var request = new XMLHttpRequest();
request.open("GET", strURL, false);
request.send(null);
if (request.responseText != "") {
document.getElementById('dv').innerHTML =
request.responseText.split('"')[1] + ' = '
+ request.responseText.split('"')[3]
}
}
</script>


HTML :

Enter Value to convert :<input type="text" id="txtVal" />
<br />
<asp:Literal ID="ltrfrom" runat="server"
Text="Select Currency to convert :">
</asp:Literal>
<asp:DropDownList ID="ddlFrom" runat="server"
Style="vertical-align: middle;">
<asp:ListItem Text="SELECT" Value="0"></asp:ListItem>
<asp:ListItem Text="US Dollar" Value="USD"></asp:ListItem>
<asp:ListItem Text="Rupees" Value="INR"></asp:ListItem>
<asp:ListItem Text="GB Pound" Value="GBP"></asp:ListItem>
<asp:ListItem Text="Australia Dollar" Value="AUD"></asp:ListItem>
<asp:ListItem Text="CAnada Dollar" Value="CAD"></asp:ListItem>
<asp:ListItem Text="Switzerland Frank" Value="CHF"></asp:ListItem>
<asp:ListItem Text="Euro" Value="EUR"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Literal ID="ltrTo" runat="server"
Text="Select Currency convert to :">
</asp:Literal>
<asp:DropDownList ID="ddlTo" runat="server"
Style="vertical-align: middle;">
<asp:ListItem Text="SELECT" Value="0"></asp:ListItem>
<asp:ListItem Text="US Dollar" Value="USD"></asp:ListItem>
<asp:ListItem Text="Rupees" Value="INR"></asp:ListItem>
<asp:ListItem Text="GB Pound" Value="GBP"></asp:ListItem>
<asp:ListItem Text="Australia Dollar" Value="AUD"></asp:ListItem>
<asp:ListItem Text="CAnada Dollar" Value="CAD"></asp:ListItem>
<asp:ListItem Text="Switzerland Frank" Value="CHF"></asp:ListItem>
<asp:ListItem Text="Euro" Value="EUR"></asp:ListItem>
</asp:DropDownList>
<br />

<input type="button" value="Get" onclick="convert();" />

<div id="dv">
</div>