Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import XML data to a Web Form and Submit data in C#

Status
Not open for further replies.

chieftain1

Technical User
May 30, 2006
6
US
I’ve created several web forms in VS.NET in C# that do similar functions (allow users to insert data in text boxes, and check boxes, then submit an XML message via SOAP). This one is a bit different. I want users to submit data from an Excel Spreadsheet generated XML file, select items in a CheckBoxList and submit the form. The Excel generated XML file does not have a set number of lines but will have repeatable data that may appear between one and 60 times. My example below includes the repeatable data five times. An XSL file exists that displays the XML file on the Web form page from an XML Control. However, I’m unable to figure out the correct C# that sends the data from the displayed XML file (see ** in C# for my attempted code). I’m also not able to send the checked items in the CheckBoxList (see ***** in C# for my attempted code). If I do not check either box in the CheckBoxList, the page runs, but the imported XML data isn’t sent I receive a 500 error from the server. I included the bold TGML Type Face for easy viewing in the C# section below.

The following is my XML template:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] xmlns:soap="[URL unfurl="true"]http://schemas.xmlsoap.org/soap/envelope/">[/URL]
  <soap:Body>
	<calTableUpdate xmlns="[URL unfurl="true"]http://www.xyz.com/CAL/CalTableUpdate/v1">[/URL]
	  <iccHeader>
		<messageType xmlns="[URL unfurl="true"]http://www.xyz.com">messagetype</messageType>[/URL]
		<originator xmlns="[URL unfurl="true"]http://www.xyz.com[/URL] ">user</originator>
		<originatorAddress xmlns="[URL unfurl="true"]http://www.xyz.com">???</originatorAddress>[/URL]
		<recipient xmlns="[URL unfurl="true"]http://www.xyz.com">receiver</recipient>[/URL]
		<creationTimeString xmlns="[URL unfurl="true"]http://www.xyz.com[/URL] ">????</creationTimeString>
		<messageId xmlns="[URL unfurl="true"]http://www.xyz.com[/URL] ">12345</messageId>
	  </iccHeader>
	  <calibrationFileList>
		<transferDirection xmlns="[URL unfurl="true"]http://www.[/URL] xyz.com ">Push</transferDirection>
		<location xmlns="[URL unfurl="true"]http://www.xyz.com">location</location>[/URL]
		<logicalName xmlns="[URL unfurl="true"]http://www.xyz.com">logicalName</logicalName>[/URL]
		<physicalName xmlns="[URL unfurl="true"]http://www.xyz.com">physicalName</physicalName>[/URL]
		<sizeInKiloBytes xmlns="[URL unfurl="true"]http://www.xyz.com">xx</sizeInKiloBytes>[/URL]
	  </calibrationFileList>
	  <disseminationList>IRS</disseminationList>
	</calTableUpdate>
  </soap:Body>
</soap:Envelope>

The following is my Excel Spreadsheet generated XML:

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<?xml-stylesheet type="text/xsl" href="TC1B_Cal5.xls.xsl"?>
<!-- This XML file is generated by RustemSoft XML Converter Evaluation version -->
<table name="TC1B_Cal">
  <TC1B_Cal>
    <transferDirection>Push</transferDirection>
	<location>[URL unfurl="true"]www.happyplace.com</location>[/URL]
    <logicalName>12347.txt</logicalName>
    <sizeInKiloBytes>20</sizeInKiloBytes>
  </TC1B_Cal>
  <TC1B_Cal>
    <transferDirection>Push</transferDirection>
	<location>[URL unfurl="true"]www.happyplace.com</location>[/URL]
    <logicalName>12348.txt</logicalName>
    <sizeInKiloBytes>14</sizeInKiloBytes>
  </TC1B_Cal>
  <TC1B_Cal>
    <transferDirection>Push</transferDirection>
	<location>[URL unfurl="true"]www.happyplace.com</location>[/URL]
    <logicalName>12349.txt</logicalName>
    <sizeInKiloBytes>11</sizeInKiloBytes>
  </TC1B_Cal>
  <TC1B_Cal>
    <transferDirection>Push</transferDirection>
	<location>[URL unfurl="true"]www.happyplace.com</location>[/URL]
    <logicalName>12345.txt</logicalName>
    <sizeInKiloBytes>8</sizeInKiloBytes>
  </TC1B_Cal>
  <TC1B_Cal>
    <transferDirection>Push</transferDirection>
	<location>[URL unfurl="true"]www.happyplace.com</location>[/URL]
    <logicalName>12346.txt</logicalName>
    <sizeInKiloBytes>17</sizeInKiloBytes>
  </TC1B_Cal>
</table>

The following is my C# code for the Web form:

Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace Forms
{
	/// <summary>
	/// Summary description for Cal_Table_Update_Form.
	/// </summary>
	public class Cal_Table_Update : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox transferDirection;
		protected System.Web.UI.WebControls.TextBox location;
		protected System.Web.UI.WebControls.TextBox logicalName;
		protected System.Web.UI.HtmlControls.HtmlForm Form1;
		protected System.Web.UI.WebControls.TextBox sizeInKiloBytes;
		protected System.Web.UI.WebControls.CheckBoxList disseminationList;
		protected System.Web.UI.WebControls.Xml calibrationFileList;
		protected System.Web.UI.WebControls.Button btnSubmit;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnSubmit_Click(object sender, System.EventArgs e)
		{
			//  load an xml document from the basic soap package file
			XmlDocument msg = ((Global)Context.ApplicationInstance).SoapSetup( "xmlCTU" );

			//  select the header node as a working element
			XmlNode hdr = msg.GetElementsByTagName( "iccHeader" )[0];

			//  set variant data in the header node sub elements
			hdr[ "originatorAddress", "[URL unfurl="true"]http://www.xyz.com"[/URL] ].InnerText = Context.Request.PhysicalPath;
			hdr[ "creationTimeString", "[URL unfurl="true"]http://www.xyz.com[/URL] " ].InnerText = DateTime.UtcNow.ToString(
				"yyyy-MM-ddTHH:mm:ss-0000" );
			hdr[ "messageId", "[URL unfurl="true"]http://www.xyz.com[/URL] " ].InnerText = Context.Request.GetHashCode().ToString();


			//  get the base calTableUpdate node for data values
			XmlNode ctu = msg.GetElementsByTagName( "calTableUpdate" )[0];

			//  get the base calTableUpdate node for data values
			XmlNode tab = msg.GetElementsByTagName( "calibrationFileList" )[0];

			[b]//  **    set user data values in the message content area
			tab[ "transferDirection" ].InnerText = transferDirection.
			tab[ "location" ].InnerText = location.Attributes.ToString();
			tab[ "logicalName" ].InnerText = logicalName.Attributes.ToString();
			tab[ "sizeInKiloBytes" ].InnerText = sizeInKiloBytes.Attributes.ToString();


			//  *****   get the base calTableUpdate node for data values
			XmlNode dil = msg.GetElementsByTagName( "disseminationList" )[0];

			//  set user data values in the message content area
			foreach(ListItem li in disseminationList.Items)
			{
				if(li.Selected == true)
				{
					dil[ "disseminationList" ].InnerText = disseminationList.SelectedValue.ToString();
				}
			}[/b]

			//  submit the document
			string rez = ((Global)Context.ApplicationInstance).SoapSubmit( msg, "adrCTU", "hdrCTU" );

			//  if successful redirect to the refering page
			if ( rez == "Success!" ) Response.Redirect( Request.UrlReferrer.AbsoluteUri );

				//  if error show message
			else Response.Write( string.Format( 
					 "<span style=\"FONT-WEIGHT: bold; COLOR: red\">{0}</span>", rez ) );

		}
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top