Sunday, July 21, 2013

How to create and read Xml in asp.net

We often have to deal with Xml message when our applications are integrated with SMS Api, Email Api or Payment Api. At that time, we prepare Xml message and feed the services. Likewise, it gets the Xml message from the service of API and extracts the message from Xml message for on-going process. This post explain step by step how to create Xml message and read the message from Xml elements.

First, copy and paste the following Xml message to new file "XmlMessage.xml" in "App_Data" folder in your project. You see "[xxxxxxx]" and it is place holder for data we put.
<?xml version="1.0" encoding="utf-8" ?>
<paymentService version="1.4" merchantCode="[merchantCode]">
  <reply>
    <orderStatus orderCode="[orderCode]">
      <payment>
        <paymentMethod>[paymentMethod]</paymentMethod>
        <amount value="[value]" currencyCode="[currencyCode]" exponent="[exponent]"/>
        <lastEvent>[lastEvent]</lastEvent>
        <CVCResultCode description="[CVCResultCode]"/>
        <ISO8583ReturnCode code="[ISO8583ReturnCode]" description="[ISO8583ReturnDescription]"/>
      </payment>
    </orderStatus>
  </reply>
</paymentService>
Second, we need to add Textbox server control to .aspx page as below:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtXml" runat="server" Height="250px" Width="576px" 
            TextMode="MultiLine"></asp:TextBox>
    </div>
    </form>
</body>
Third, copy and paste the following code in code behind.
protected void Page_Load(object sender, EventArgs e)
{
    //Set the data here.
    string merchantCode = "MYMERCHANT";
    string orderCode = "T0211234";
    string paymentMethod = "ECMC-SSL";
    string value = "162095";
    string currencyCode = "GPB";
    string exponent = "2";
    string lastEvent = "REFUSED";
    string CVCResultCode = "NOT SUPPLIED BY SHOPPER";
    string ISO8583ReturnCode = "33";
    string ISO8583ReturnDescription = "CARD EXPIRED";

    string xmlMsg = GetXmlMessage();
    //Fill the data in place holder in Xml message.
    xmlMsg = xmlMsg.Replace("[merchantCode]", merchantCode);
    xmlMsg = xmlMsg.Replace("[orderCode]", orderCode);
    xmlMsg = xmlMsg.Replace("[paymentMethod]", paymentMethod);
    xmlMsg = xmlMsg.Replace("[value]", value);
    xmlMsg = xmlMsg.Replace("[currencyCode]", currencyCode);
    xmlMsg = xmlMsg.Replace("[exponent]", exponent);
    xmlMsg = xmlMsg.Replace("[lastEvent]", lastEvent);
    xmlMsg = xmlMsg.Replace("[CVCResultCode]", CVCResultCode);
    xmlMsg = xmlMsg.Replace("[ISO8583ReturnCode]", ISO8583ReturnCode);
    xmlMsg = xmlMsg.Replace("[ISO8583ReturnDescription]", ISO8583ReturnDescription);
    txtXml.Text = xmlMsg;
    ReadXmlMessage(xmlMsg);
}

public string GetXmlMessage()
{
    //Get Xml message from file.
    string XmlFile = "~/App_Data/XmlMessage.xml";
    StreamReader sr = new StreamReader(Server.MapPath(XmlFile));
    string str = sr.ReadToEnd();
    return str;
}

public void ReadXmlMessage(string xmlMsg)
{
    if (!string.IsNullOrEmpty(xmlMsg))
    {
        //Read the data from Xml elements.
        XmlDocument document = new XmlDocument();
        document.LoadXml(xmlMsg);
        string merchantCode = document.GetElementsByTagName("paymentService")[0].Attributes["merchantCode"].Value;
        string orderCode = document.GetElementsByTagName("orderStatus")[0].Attributes["orderCode"].Value;
        string paymentMethod = document.SelectSingleNode("//paymentMethod").InnerText;
        string value = document.GetElementsByTagName("amount")[0].Attributes["value"].Value;
        string currencyCode = document.GetElementsByTagName("amount")[0].Attributes["currencyCode"].Value;
        string exponent = document.GetElementsByTagName("amount")[0].Attributes["exponent"].Value;
        string lastEvent = document.SelectSingleNode("//lastEvent").InnerText;
        string CVCResultCode = document.GetElementsByTagName("CVCResultCode")[0].Attributes["description"].Value;
        string ISO8583ReturnCode = document.SelectSingleNode("//ISO8583ReturnCode").Attributes["code"].Value;
        string ISO8583ReturnDescription = document.SelectSingleNode("//ISO8583ReturnCode").Attributes["description"].Value;
    }
}
I use XmlNode.SelectSingleNode method to get xml data. It selects the first XmlNode that matches the XPath expression. The first XmlNode that matches the XPath query or null if no matching node is found.
I used aslo XmlDocument.GetElementsByTagName method to return an XmlNodeList containing a list of all descendant elements that match the specified Name. If no nodes match name, the returned collection will be empty.

Finally, run and see the result. It look like as below:

No comments:

Post a Comment