Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every one,
am newly enter in xml, i dont know whole thing , even though i know few things how do i call xml in to c# code. in this i have few douts , am developing one application using asp.net with c# language,
the concept which is nothing but the how to insert text box value into xml using c#.net ,
acually i need the below output,

my sample output is:
XML
<?xml version="1.0" encoding="utf-8"?>
<requestblock version="3.67">
  <alias>testing</alias>
  <request type="STORE">
    <merchant>
      <orderreference>EXAMPLE CARDSTORE</orderreference>
    </merchant>
    <operation>
      <sitereference>live2</sitereference>
      <accounttypedescription>CARDSTORE</accounttypedescription>
    </operation>
    <billing>
      <town>mumbai</town>
      <street>nehru nagr</street>
      <postcode>6005432</postcode>
      <premise>564</premise>
      <payment type="VISA">
        <pan>411123445234556677</pan>
        <expirydate>10/13</expirydate>
      </payment>
      <name>
        <first>test</first>
      </name>
    </billing>
  </request>
</requestblock>


the thing is am getting the output which is in the unordered place, please find out my below code and let me know where i did mistake and give me a solution please...

Defaault.aspx.cs:


C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class sendrequest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        XmlDocument myxmldocument = new XmlDocument();
        myxmldocument.Load(Server.MapPath("request.xml"));
        XmlNode myxmlnode = myxmldocument.DocumentElement.FirstChild;
        XmlElement myxmlelement = myxmldocument.CreateElement("billing");
        myxmlelement.SetAttribute("town", Server.HtmlEncode(txttown.Text));
        myxmlelement.SetAttribute("street", Server.HtmlEncode(txtstreet.Text));
        myxmlelement.SetAttribute("postcode", Server.HtmlEncode(txtpostcode.Text));
        myxmlelement.SetAttribute("premise", Server.HtmlEncode(txtpremise.Text));
        myxmlelement.SetAttribute("pan", Server.HtmlEncode(txtpan.Text));
        myxmlelement.SetAttribute("expirydate", Server.HtmlEncode(txtexpiry.Text));
        myxmldocument.DocumentElement.InsertBefore(myxmlelement, myxmlnode);
        myxmldocument.Save(Server.MapPath("request.xml"));
        binddata();
        lblwelcome.Text = "Record inserted Successfully Inside the XML Fil";
        txttown.Text  = "";
        txtstreet.Text  = "";
        txtpremise.Text = "";
        txtpostcode.Text = "";
        txtpan.Text = "";
        txtexpiry.Text = ""; 
    }
    private void binddata()
    {
        XmlTextReader myxmlreader = new XmlTextReader(Server.MapPath("request.xml"));
        myxmlreader.Close();
    }
}



request.xml:

XML
<?xml version="1.0" encoding="utf-8"?>
<requestblock version="3.67">
  <alias>testing</alias>
  <request type="STORE">
    <merchant>
      <orderreference>EXAMPLE CARDSTORE</orderreference>
    </merchant>
    <operation>
      <sitereference>live2</sitereference>
      <accounttypedescription>CARDSTORE</accounttypedescription>
    </operation>
    <billing>
      <town></town>
      <street></street>
      <postcode></postcode>
      <premise></premise>
      <payment type="VISA">
        <pan></pan>
        <expirydate></expirydate>
      </payment>
      <name>
        <first></first>
      </name>
    </billing>
  </request>
</requestblock>


the above code which was done by the mistakes output is:

XML
<?xml version="1.0" encoding="utf-8"?>
<requestblock version="3.67">
  <billing town="chennai" street="anna nagar" postcode="600034" premise="878" pan="411111887777766723" expirydate="10/13" />
  <alias>testing</alias>
  <request type="STORE">
    <merchant>
      <orderreference>EXAMPLE CARDSTORE</orderreference>
    </merchant>
    <operation>
      <sitereference>live2</sitereference>
      <accounttypedescription>CARDSTORE</accounttypedescription>
    </operation>
    <billing>
      <town>
      </town>
      <street>
      </street>
      <postcode>
      </postcode>
      <premise>
      </premise>
      <payment type="VISA">
        <pan>
        </pan>
        <expirydate>
        </expirydate>
      </payment>
      <name>
        <first>
        </first>
      </name>
    </billing>
  </request>
</requestblock>


so please follow my code and give me a solution....


thanks in advance....
Posted
Updated 17-Feb-13 22:37pm
v3

1 solution

 
Share this answer
 
Comments
stellus 18-Feb-13 5:01am    
thank you very much which is really helpful for me which you provide the above link
Tejas Vaishnav 18-Feb-13 5:03am    
if your question is solved then please accept the solutions as your answer, so it will helpful to other to identify problem solution.
Avik Ghosh22 18-Feb-13 5:29am    
welcome bro...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900