This solution is very similar to the JSON solution I wrote about, with a few changes to apply to XML. The XML source we're using is going to apply to metrics used in a call center. These attributes include employee login, date, calls offered, calls answered and calls missed. Our XML source will look something like this:
<?xml version="1.0" encoding="utf-8"?>
<DisplayStats>
<DailyStat>
<WebLogin>eli</WebLogin>
<Date>2013-03-28T00:00:00</Date>
<CallsOffered>4</CallsOffered>
<CallsAnswered>4</CallsAnswered>
<CallsMissed>0</CallsMissed>
</DailyStat>
<DailyStat>
<WebLogin>rit</WebLogin>
<Date>2013-03-28T00:00:00</Date>
<CallsOffered>0</CallsOffered>
<CallsAnswered>0</CallsAnswered>
<CallsMissed>0</CallsMissed>
</DailyStat>
<DailyStat>
<WebLogin>Dan</WebLogin>
<Date>2013-03-28T00:00:00</Date>
<CallsOffered>15</CallsOffered>
<CallsAnswered>15</CallsAnswered>
<CallsMissed>1</CallsMissed>
</DailyStat>
<DailyStat>
<WebLogin>Pab</WebLogin>
<Date>2013-03-28T00:00:00</Date>
<CallsOffered>0</CallsOffered>
<CallsAnswered>0</CallsAnswered>
<CallsMissed>0</CallsMissed>
</DailyStat>
<DailyStat>
<WebLogin>Rus</WebLogin>
<Date>2013-03-28T00:00:00</Date>
<CallsOffered>21</CallsOffered>
<CallsAnswered>21</CallsAnswered>
<CallsMissed>0</CallsMissed>
</DailyStat>
</DisplayStats>
This comes from an ASP.NET web service I built using SOAP:
Figure 1. XML String |
You can see from this screenshot that the web service takes one parameter, offerDate, with a date format. We're going to want to make this a variable that we can alter at run time every time this package is executed. This can be done through values returned from an Execute Sql Task from a database, set via a function, etc. Regardless of how its implemented, in this example were going to hard code the variable to 03/28/2013:
Figure 3. Script Component Type |
On the script screen, we want to make sure to add our SSIS variable in ReadOnlyVariables:
Figure 4. Script Component Script Screen |
This screen helps us to define what our output is going to look like. The columns that will represent our XML elements are as follows: WEBLOGIN DT_STR 50, DATE DT_DBTIMESTAMP, CALLSOFFERED DT_I4, CALLSANSWERED DT_I4 and CALLSMISSED DT_I4. With this all set up, we can go back to the script screen, click on the Edit Script button and begin coding. In the main.cs file, paste the following code:
#region Namespaces
using System;
using System.Data;
using
Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using
Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Xml.Serialization;
using System.Net;
using System.IO;
#endregion
#region Class
/// <summary>
/// This is the class to which
to add your code. Do not change the
name, attributes, or parent
/// of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Methods
/// <summary>Outputs records to the output
buffer</summary>
public override void CreateNewOutputRows()
{
//Get SSIS Variables
string offerDate = Variables.offerDate;
//Set Webservice URL
string wUrl = "http://yourwebserviceurl/Respiratory/ResService.asmx/getAgentMetricsXml?offerDate=" + offerDate;
try
{
//Call getWebServiceResult to return our
DailyStat array
DailyStats outPutStats =
GetWebServiceResult(wUrl);
//For each group of metrics output
records
foreach (var stats in outPutStats.MDailyStat)
{
Output0Buffer.AddRow();
Output0Buffer.WEBLOGIN = stats.WebLogin;
Output0Buffer.DATE =stats.Date;
Output0Buffer.CALLSANSWERED
=stats.CallsAnswered;
Output0Buffer.CALLSOFFERED =
stats.CallsOffered;
Output0Buffer.CALLSMISSED =
stats.CallsMissed;
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
/// <summary>
/// Method to call the web service and
return our DailyStat array
/// </summary>
/// <param name="wUrl">The web servie URL</param>
/// <returns>An array of DailyStat which
contains the deserialized XML</returns>
private DailyStats GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
DailyStats xmlResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream =
httpWResp.GetResponseStream();
//Set xmlString using a
stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our XML
XmlSerializer sr = new XmlSerializer(typeof(DailyStats));
xmlResponse = (DailyStats)sr.Deserialize(reader);
reader.Close();
}
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output xml parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return xmlResponse;
}
/// <summary>
/// Outputs an error message
/// </summary>
/// <param name="errorMsg">The error message</param>
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data
From Webservice!", errorMsg, "", 0, out fail);
}
#endregion
}
#endregion
#region XML Classes
//Class to hold elements
within <DailyStat>
public class DailyStat
{
public string WebLogin { get; set; }
public DateTime Date { get; set; }
public int CallsOffered { get; set; }
public int CallsAnswered { get; set; }
public int CallsMissed { get; set; }
}
//Class to hold our array of
<DailyStat>
[XmlRootAttribute("DisplayStats")]
public class DailyStats
{
[XmlElement("DailyStat")]
public DailyStat[] MDailyStat { get; set; }
}
#endregion
After calling our web service and getting our stream of XML, we deserialize into a DailyStats object(Creating the classes for your XML to deserialize into can be done by copying a sample of your XML into your clipboard then pasting special into a .net 4.5 project in VS2012 with edit -->paste special-->Paste XML as Classes):
//Deserialize our XML
XmlSerializer sr = new XmlSerializer(typeof(DailyStats));
xmlResponse = (DailyStats)sr.Deserialize(reader);
This object is returned to the calling method. We then loop through the DailyStat[] array, within the DailyStats object, and output the attributes to the output buffer:
//For each group of metrics output records
foreach (var stats in outPutStats.MDailyStat)
{
Output0Buffer.AddRow();
Output0Buffer.WEBLOGIN = stats.WebLogin;
Output0Buffer.DATE =stats.Date;
Output0Buffer.CALLSANSWERED =stats.CallsAnswered;
Output0Buffer.CALLSOFFERED = stats.CallsOffered;
Output0Buffer.CALLSMISSED = stats.CallsMissed;
}
Build, save and lets return to the data flow. We can create a union all that the script component can output to so we can add a data viewer to see the records. In a normal implementation this would go to a data destination:
Figure 6. Data Flow |
Let's now run the package and view the results:
Figure 7. Data Viewer |
The XML data is now in our data flow and can be sent to a data destination. No physical XML file was needed, which means we didn't have to rely on an XML Source Component to get this.
Hi,,
ReplyDeleteCan you please let me know where i have to put this code:
//After calling our web service and getting our stream of XML, we deserialize into a DisplayStats object:
System.Xml.XmlSerialization.XmlSerializer sr = new XmlSerializer(typeof(OUTPUT));
xmlResponse = (DisplayStats)sr.Deserialize(reader);
//This object is returned to the calling method. We then loop through the DailyStat[]array, within the DisplayStats object, and output the attributes to the output buffer:
//Call getWebServiceResult to return our DailyStat array
OUTPUT outPutStats = getWebServiceResult(wUrl);
//For each group of metrics output records
foreach (var stats in outPutStats.MDailyStat)
{
Output0Buffer.AddRow();
//Output0Buffer.WEBLOGIN = stats.WebLogin;
Output0Buffer.Date = stats.Date;
Output0Buffer.Unsub = stats.Unsub;
Output0Buffer.Abuse = stats.Abuse;
//Output0Buffer.CALLSMISSED = stats.CallsMissed;
}
I am new to c#,
Pls help me out
You do all of your coding in the script component. Can you show me all of your code so far? Can you show me a sample of the XML your'e dealing with?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi Jim.
ReplyDeleteI am getting following errors:
Error 1 A namespace does not directly contain members such as fields or methods
Error 2 Expected class, delegate, enum, interface, or struct
Xml:
success
2013-06-12
477
23
I think I can see what the issue is....you need to use the xml node names provided...not what I have in the example. My example uses the xml in this blog, you need to make your code match whatever your xml is. For instance, you are still WebLogin, is that in your xml? If you post a sample of the xml that is being returned from you web service I can help you debug this. You can change the values if you want, so it won't contain proprietary data, but I would like to see what you're trying to deserialize.
ReplyDeleteAPI is returning following :
ReplyDeletesuccess
2013-06-12
477
23
Nevermind...I see you have weblogin commented out, I still would like to see the xml please.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteSo your web service doesn't provide xml? If not then you shouldn't try to use XmlSerializer on simple text and would need a different solution.
ReplyDeleteIf it does then can you download Simple Rest Client (https://chrome.google.com/webstore/detail/simple-rest-client/fhjcajmcbmldlhcimfajhfbgofnpcjmb?hl=en)
and tell me the format of the xml returned.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi Jim,
ReplyDeleteCan you please send me SSIS package you developed.
My email id : maneeshhbtik@gmail.com
Does OUTPUT only occur once per DATASET or can it show up multiple times? I assume there can be multiple RECORD nodes in OUTPUT.
ReplyDeleteyes there can be multiple RECORD nodes in OUTPUT
ReplyDelete//GOT IT!
ReplyDeleteusing System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void CreateNewOutputRows()
{
//Set Webservice URL
string wUrl = "https://api.extouch.com";
try
{
//Call getWebServiceResult to return our DailyStat array
DataSet outPutStats = getWebServiceResult(wUrl);
//For each output
foreach (var stats in outPutStats.MoutPut)
{
//For each group of metrics output records
foreach (var records in stats.Record)
{
Output0Buffer.AddRow();
Output0Buffer.Date = records.Date;
Output0Buffer.Unsub = records.Unsub;
Output0Buffer.Abuse = records.Abuse;
}
}
}
catch (Exception e)
{
failComponent(e.ToString());
}
}
//Method to return our DailyStat array
private DataSet getWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
DataSet xmlResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
MessageBox.Show("success");
//Set xmlString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our XML
XmlSerializer sr = new XmlSerializer(typeof(DataSet));
xmlResponse = (DataSet)sr.Deserialize(reader);
reader.Close();
}
}
//Output connection error message
else
{
failComponent(httpWResp.StatusCode.ToString());
}
}
//Output xml parsing error
catch (Exception e)
{
failComponent(e.ToString());
}
return xmlResponse;
}
//Outputs error message
private void failComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
}
public class OUTPUT
{
[XmlElement("RECORD")]
public RECORD[] Record { get; set; }
}
public class RECORD
{
public DateTime Date { get; set; }
public int Unsub { get; set; }
public int Abuse { get; set; }
}
//Class to hold our array of
[XmlRootAttribute("DATASET")]
public class DataSet
{
[XmlElement("OUTPUT")]
public OUTPUT[] MoutPut { get; set; }
[XmlElement("TYPE")]
string type { get; set; }
}
Hi Jim,
ReplyDeleteThanks a lot for your help!!
Got the output.
No Problem man...good luck with your project.
ReplyDeleteOk, got this working. Was a little tricky because the paste XML as classes in .net 4.5 didn't like this XML. Anyways...here's how to get this working.
ReplyDelete1. Create 3 outputs on the Inputs and Outputs screen of the script component that contain this:
source
publisher DT_STR 50
publisherurl DT_STR 100
lastBuildDate DT_STR 100
sourceJob
country_short DT_STR 100
city DT_STR 100
description DT_TEXT
date_new DT_STR 50
country DT_STR 50
company DT_STR 200
title DT_STR 200
state DT_STR 50
reqid DT_STR 100
state_short DT_STR 50
location DT_STR 100
guid DT_STR 100
uid DT_STR 100
url DT_STR 100
sourceLink
href DT_STR 100
rel DT_STR 100
2. Use this code in your script component:
ReplyDelete#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Xml.Serialization;
using System.Net;
using System.IO;
using System.Text;
#endregion
#region Class
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Methods
public override void CreateNewOutputRows()
{
//Set Webservice URL
string wUrl = "http://sfngroup.jobs/developer/jobs-in/feed/xml?num_items=100&offset=0";
try
{
//Call getWebServiceResult to return our source object
source outputSource = GetWebServiceResult(wUrl);
//output source attributes
sourceBuffer.AddRow();
sourceBuffer.lastBuildDate=outputSource.lastBuildDate;
sourceBuffer.publisher=outputSource.publisher;
sourceBuffer.publisherurl=outputSource.publisherurl;
////For each link in source
foreach (var links in outputSource.link)
{
sourceLinkBuffer.AddRow();
sourceLinkBuffer.href = links.href;
sourceLinkBuffer.rel = links.rel;
}
//For each job in source
foreach (var jobs in outputSource.job)
{
sourceJobBuffer.AddRow();
sourceJobBuffer.city=jobs.city;
sourceJobBuffer.company=jobs.company;
sourceJobBuffer.country=jobs.country;
sourceJobBuffer.countryshort=jobs.country_short;
sourceJobBuffer.datenew=jobs.date_new;
if (jobs.description != null)
{
sourceJobBuffer.description.AddBlobData(Encoding.Unicode.GetBytes(jobs.description));
}
//Handle if text is null
else
{
sourceJobBuffer.description.SetNull();
}
sourceJobBuffer.guid=jobs.guid;
sourceJobBuffer.location=jobs.location;
sourceJobBuffer.reqid=jobs.reqid;
sourceJobBuffer.state=jobs.state;
sourceJobBuffer.stateshort=jobs.state_short;
sourceJobBuffer.title=jobs.title;
sourceJobBuffer.uid=jobs.uid;
sourceJobBuffer.url=jobs.url;
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
private source GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
source xmlResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our XML
XmlSerializer sr = new XmlSerializer(typeof(source));
xmlResponse = (source)sr.Deserialize(reader);
reader.Close();
}
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output xml parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return xmlResponse;
}
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
#endregion
}
#endregion
#region XML Classes
ReplyDelete[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class source
{
private string publisherField;
private string publisherurlField;
private string lastBuildDateField;
private Link[] linkField;
private Job[] jobField;
public string publisher
{
get
{
return this.publisherField;
}
set
{
this.publisherField = value;
}
}
public string publisherurl
{
get
{
return this.publisherurlField;
}
set
{
this.publisherurlField = value;
}
}
public string lastBuildDate
{
get
{
return this.lastBuildDateField;
}
set
{
this.lastBuildDateField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("link")]
public Link[] link
{
get
{
return this.linkField;
}
set
{
this.linkField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("job")]
public Job[] job
{
get
{
return this.jobField;
}
set
{
this.jobField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class Link
{
private string hrefField;
private string relField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string href
{
get
{
return this.hrefField;
}
set
{
this.hrefField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string rel
{
get
{
return this.relField;
}
set
{
this.relField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
ReplyDeletepublic partial class Job
{
private string country_shortField;
private string cityField;
private string descriptionField;
private string date_newField;
private string countryField;
private string companyField;
private string titleField;
private string stateField;
private string reqidField;
private string state_shortField;
private string locationField;
private string guidField;
private string uidField;
private string urlField;
public string country_short
{
get
{
return this.country_shortField;
}
set
{
this.country_shortField = value;
}
}
public string city
{
get
{
return this.cityField;
}
set
{
this.cityField = value;
}
}
public string description
{
get
{
return this.descriptionField;
}
set
{
this.descriptionField = value;
}
}
public string date_new
{
get
{
return this.date_newField;
}
set
{
this.date_newField = value;
}
}
public string country
{
get
{
return this.countryField;
}
set
{
this.countryField = value;
}
}
public string company
{
get
{
return this.companyField;
}
set
{
this.companyField = value;
}
}
public string title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
public string state
{
get
{
return this.stateField;
}
set
{
this.stateField = value;
}
}
public string reqid
{
get
{
return this.reqidField;
}
set
{
this.reqidField = value;
}
}
public string state_short
{
get
{
return this.state_shortField;
}
set
{
this.state_shortField = value;
}
}
public string location
{
get
{
return this.locationField;
}
set
{
this.locationField = value;
}
}
public string guid
{
get
{
return this.guidField;
}
set
{
this.guidField = value;
}
}
public string uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
public string url
{
get
{
return this.urlField;
}
set
{
this.urlField = value;
}
}
}
#endregion
Sorry I had to break out the code into sections since there is a max allowable char count for comments in blogger:
ReplyDelete3. Send each output to data destinations that match the structure of the output. i.e. DT_STR 50 = varchar(50), DT_TEXT = varchar(max),etc.
I didn't add the ssis variables to the code, I think you can figure that part out. If not reply back.
ReplyDeleteNp man, sorry I didn't help, just saw the comments now. But it sounds like you worked through it yourself anyways. Gl with your project. Reply back if you run into another snag. Looping through record offsets can be tricky in some web api's and I don't know if this site offers documentation at all for it.
ReplyDeleteHi Jim,
ReplyDeleteGreat post here but I am struggling for a day now...
I am trying to read from a webservice that needs credentials (username, password and contract number), I guess this is the reason why I don't get anything back in the dataviewer.
Where should I pass these credentials?
Any help is appreciated :)
What webservice is this? Do they have any API documentation?
DeleteYeah I had to contact them and get some sample codes (only VB though). It was strange it didn't work in C# :) But I got it now...
DeleteThanks for your time anyway :)
Could you please let me know .. how do u provide the credentials. This is same with me
DeleteHi Jim/Pepys,
DeleteCould you please help me with this? I am running into the same issue. I need to add basic authentication in the code.
Hello! this is quite an useful code. Im new in this and I've been struggling trying to make it work in this site:
ReplyDeletehttp://api.sbif.cl/api-sbifv3/recursos_api/uf/periodo/2014/09/2014/10?apikey=208fba343b0a69bc08f291cc8d09eda62d4d3fa3&formato=xml
Tthe catch is, the "2014/09/2014/10" part of the code must change every month and I just couldn't get it to work. I would much appreciate your help.
Do you have some code developed already? Have you tried making some SSIS variables for the dates, handle changing the variables in the SSIS package and passing those variables to the script component?
DeleteThis comment has been removed by the author.
ReplyDeleteI think I'm very close to implementing your method... my complexity appears similar to the person that you helped a year ago (upordownnow above). I have to post in 4 parts.
ReplyDeletetry
{
//Call getWebServiceResult to return our DailyCall array
Callmeasurement outPutStats = GetWebServiceResult(wUrl);
//For each group of metrics output records
foreach (var call in outPutStats.MCall)
{
Output0Buffer.AddRow();
Output0Buffer.callid = call.callid;
Output0Buffer.siteid = call.siteid;
Output0Buffer.date = call.date;
Output0Buffer.time = call.time;
Output0Buffer.timetoanswer = call.timetoanswer;
Output0Buffer.duration = call.duration;
Output0Buffer.billedminutes = call.billedminutes;
Output0Buffer.disposition = call.disposition;
Output0Buffer.ringto = call.ringto;
Output0Buffer.dnisnumber = call.dnisnumber;
Output0Buffer.dnislabel1 = call.dnislabel1;
Output0Buffer.dnislabel2 = call.dnislabel2;
Output0Buffer.dnislabel1 = call.dnislabel3;
Output0Buffer.dnislabel2 = call.dnislabel4;
Output0Buffer.dnislabel1 = call.dnislabel5;
Output0Buffer.dnislabel2 = call.dnislabel6;
Output0Buffer.dnislabel1 = call.dnislabel7;
Output0Buffer.dnislabel2 = call.dnislabel8;
Output0Buffer.dnislabel1 = call.dnislabel9;
Output0Buffer.dnislabel2 = call.dnislabel10;
Output0Buffer.dnislabel1 = call.dnislabel11;
Output0Buffer.dnislabel2 = call.dnislabel12;
Output0Buffer.dnislabel1 = call.dnislabel13;
Output0Buffer.dnislabel2 = call.dnislabel14;
Output0Buffer.dnislabel1 = call.dnislabel15;
Output0Buffer.custnumber = call.custnumber;
Output0Buffer.custcity = call.custcity;
Output0Buffer.custstate = call.custstate;
Output0Buffer.custpostalcode = call.custpostalcode;
Output0Buffer.phonecodeid = call.phonecodeid;
Output0Buffer.phonecodename = call.phonecodename;
Output0Buffer.audio = call.audio;
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
private Callmeasurement GetWebServiceResult(string wUrl)
ReplyDelete{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
Callmeasurement xmlResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
//Set xmlString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our XML
XmlSerializer sr = new XmlSerializer(typeof(Callmeasurement));
xmlResponse = (Callmeasurement)sr.Deserialize(reader);
reader.Close();
}
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output xml parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return xmlResponse;
}
///
/// Outputs an error message
///
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
#endregion
}
#endregion
///
ReplyDelete[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class callmeasurement
{
private callmeasurementCall callField;
///
public callmeasurementCall call
{
get
{
return this.callField;
}
set
{
this.callField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCall
{
private ushort site_idField;
private System.DateTime dateField;
private string timeField;
private byte time_to_answerField;
private System.DateTime durationField;
private decimal billed_minutesField;
private string dispositionField;
private uint ring_toField;
private callmeasurementCallDnis dnisField;
private callmeasurementCallCustomer customerField;
private callmeasurementCallPhonecode phonecodeField;
private string audioField;
private ulong idField;
///
public ushort site_id
{
get
{
return this.site_idField;
}
set
{
this.site_idField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime date
{
get
{
return this.dateField;
}
set
{
this.dateField = value;
}
}
///
public string time
{
get
{
return this.timeField;
}
set
{
this.timeField = value;
}
}
///
public byte time_to_answer
{
get
{
return this.time_to_answerField;
}
set
{
this.time_to_answerField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(DataType = "time")]
public System.DateTime duration
{
get
{
return this.durationField;
}
set
{
this.durationField = value;
}
}
///
public decimal billed_minutes
{
get
{
return this.billed_minutesField;
}
set
{
this.billed_minutesField = value;
}
}
///
public string disposition
{
get
{
return this.dispositionField;
}
set
{
this.dispositionField = value;
}
}
///
public uint ring_to
{
get
{
return this.ring_toField;
}
set
{
this.ring_toField = value;
}
}
///
public callmeasurementCallDnis dnis
{
get
{
return this.dnisField;
}
set
{
this.dnisField = value;
}
}
///
public callmeasurementCallCustomer customer
{
get
{
return this.customerField;
}
set
{
this.customerField = value;
}
}
///
public callmeasurementCallPhonecode phonecode
{
get
{
return this.phonecodeField;
}
set
{
this.phonecodeField = value;
}
}
///
public string audio
{
get
{
return this.audioField;
}
set
{
this.audioField = value;
}
}
///
ReplyDelete[System.Xml.Serialization.XmlAttributeAttribute()]
public ulong id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallDnis
{
private string numberField;
private callmeasurementCallDnisLabel[] labelField;
///
public string number
{
get
{
return this.numberField;
}
set
{
this.numberField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute("label")]
public callmeasurementCallDnisLabel[] label
{
get
{
return this.labelField;
}
set
{
this.labelField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallDnisLabel
{
private byte placeField;
private string valueField;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte place
{
get
{
return this.placeField;
}
set
{
this.placeField = value;
}
}
///
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallCustomer
{
private ulong numberField;
private string cityField;
private string stateField;
private ushort postalcodeField;
///
public ulong number
{
get
{
return this.numberField;
}
set
{
this.numberField = value;
}
}
///
public string city
{
get
{
return this.cityField;
}
set
{
this.cityField = value;
}
}
///
public string state
{
get
{
return this.stateField;
}
set
{
this.stateField = value;
}
}
///
public ushort postalcode
{
get
{
return this.postalcodeField;
}
set
{
this.postalcodeField = value;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallPhonecode
{
private string idField;
private string nameField;
///
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
///
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
FYI- I didn't post the beginning of the script because of the length. I can run a previous version successfully, but most my columns contain null values.
ReplyDeleteI realized I needed the entire doc when Paste as XML Classes. Following class changed...
ReplyDeletepublic partial class callmeasurement
{
private callmeasurementCall[] callField;
///
[System.Xml.Serialization.XmlElementAttribute("call")]
public callmeasurementCall[] call
{
get
{
return this.callField;
}
set
{
this.callField = value;
}
}
}
Do you still need help? Can you share some sample XML so I can help troubleshoot?
DeleteHelp would be great. The following is a sample of a single call from a daily feed for which there are 1000+ calls. I substitute </> for tags in order to post.
Delete<?xml version="1.0" encoding="UTF-8"?%gt
<callmeasurement%gt
<call id="11111111111"%gt
<site_id%gt99999</site_id%gt
<date%gt2015-06-01</date%gt
<time%gt07:21:47 AM</time%gt
<time_to_answer%gt4</time_to_answer%gt
<duration%gt00:00:10</duration%gt
<billed_minutes%gt0.33</billed_minutes%gt
<disposition%gtAnswered</disposition%gt
<ring_to%gt7777777777</ring_to%gt
<dnis%gt
<number%gt123-123-1234</number%gt
<label place="1"%gt99999-FirstName LastName</label%gt
<label place="2"%gt111111 Product(Y)</label%gt
<label place="3"%gtDTL</label%gt
<label place="4"%gt$891.00</label%gt
<label place="5"%gt6/1/2014</label%gt
<label place="6"%gt3/7/2014</label%gt
<label place="7"%gtPublisher</label%gt
<label place="8"%gtProductBrand</label%gt
<label place="9"%gtCorporate</label%gt
<label place="10"%gt06/01/2014</label%gt
<label place="12"%gt$890.80</label%gt
<label place="13"%gtCorporate</label%gt
<label place="14"%gt2013-06-01</label%gt
<label place="15"%gtRef end 8-31-13 Ported-10-30-2012</label%gt
</dnis%gt
<customer%gt
<number%gt8888888888</number%gt
<city%gtCAMDEN-CENTRAL</city%gt
<state%gtNJ</state%gt
<postalcode%gt12345</postalcode%gt
</customer%gt
<phonecode%gt
<id%gtunknown</id%gt
<name%gtunknown</name%gt
</phonecode%gt
<audio%gthttp://www.url.com/review_x.cfm?cid=11111111111&lid=99999</audio%gt
</call%gt
</callmeasurement%gt
Here's how I got this to work.
DeleteI created 2 outputs on the inputs and outputs screen:
callmeasurement
call_id DT_I8
site_id DT_I4
date DT_DBTIMESTAMP
time DT_STR 11
time_to_answer DT_I4
duration DT_STR 8
billed_minutes DT_DECIMAL
disposition DT_STR 50
ring_to DT_I8
dnis_number DT_STR 12
customer_number DT_I8
customer_city DT_STR 50
customer_postalcode DT_I4
phonecode_id DT_STR 50
phonecode_name DT_STR 50
audio DT_STR 100
label
call_id DT_I8
dnis_number DT_STR 12
label_place DT_I4
label_value DT_STR 50
You can join callmeasurement to label on call_id and dnis_number. The code is:
#region Namespaces
Deleteusing System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Xml.Serialization;
using System.Net;
using System.IO;
#endregion
#region Class
/// < summary >
/// This is the class to which to add your code. Do not change the name, attributes, or parent
/// of this class.
/// < /summary >
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Methods
/// < summary >Outputs records to the output buffer< /summary >
public override void CreateNewOutputRows()
{
//Set Webservice URL
string wUrl = "YOUR URL GOES HERE";
try
{
//Call getWebServiceResult to return our callmeasurement object
callmeasurement outPutCallMeasurement = GetWebServiceResult(wUrl);
if(outPutCallMeasurement != null)
{
callmeasurementBuffer.AddRow();
callmeasurementBuffer.callid=outPutCallMeasurement.call.id;
callmeasurementBuffer.siteid=outPutCallMeasurement.call.site_id;
callmeasurementBuffer.date=outPutCallMeasurement.call.date;
callmeasurementBuffer.time=outPutCallMeasurement.call.time;
callmeasurementBuffer.timetoanswer=outPutCallMeasurement.call.time_to_answer;
callmeasurementBuffer.duration=outPutCallMeasurement.call.duration;
callmeasurementBuffer.billedminutes=outPutCallMeasurement.call.billed_minutes;
callmeasurementBuffer.disposition=outPutCallMeasurement.call.disposition;
callmeasurementBuffer.ringto=outPutCallMeasurement.call.ring_to;
callmeasurementBuffer.dnisnumber=outPutCallMeasurement.call.dnis.number;
callmeasurementBuffer.customernumber=outPutCallMeasurement.call.customer.number;
callmeasurementBuffer.customercity=outPutCallMeasurement.call.customer.city;
callmeasurementBuffer.customerpostalcode=outPutCallMeasurement.call.customer.postalcode;
callmeasurementBuffer.phonecodeid=outPutCallMeasurement.call.phonecode.id;
callmeasurementBuffer.phonecodename=outPutCallMeasurement.call.phonecode.name;
callmeasurementBuffer.audio=outPutCallMeasurement.call.audio;
foreach(callmeasurementCallDnisLabel label in outPutCallMeasurement.call.dnis.label)
{
labelBuffer.AddRow();
labelBuffer.callid=outPutCallMeasurement.call.id;
labelBuffer.dnisnumber=outPutCallMeasurement.call.dnis.number;
labelBuffer.labelplace=label.place;
labelBuffer.labelvalue=label.value;
}
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
/// < summary >
Delete/// Method to call the web service and return our callmeasurement object
/// < /summary >
/// < param name="wUrl" >The web service URL< /param >
/// < returns >An array of call measurement object which contains the deserialized XML< /returns >
public callmeasurement GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
callmeasurement xmlResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
//Set xmlString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
XmlSerializer sr = new XmlSerializer(typeof(callmeasurement));
using (var sReader = new StringReader(reader.ReadToEnd().Replace("&","&")))
{
xmlResponse = (callmeasurement)sr.Deserialize(sReader);
}
}
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output xml parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return xmlResponse;
}
/// < summary >
Delete/// Outputs an error message
/// < /summary >
/// < param name="errorMsg" >The error message< /param >
public void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
#endregion
}
#endregion
#region XML Classes
//< callmeasurement >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class callmeasurement
{
public callmeasurementCall call;
}
//< call >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCall
{
//< site_id >
public int site_id;
//< date >
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime date;
//< time >
public string time;
//< time_to_answer >
public byte time_to_answer;
//< duration >
public string duration;
//< billed_minutes >
public decimal billed_minutes;
//< disposition >
public string disposition;
//< ring_to >
public long ring_to;
//< dnis >
public callmeasurementCallDnis dnis;
//< customer >
public callmeasurementCallCustomer customer;
//
public callmeasurementCallPhonecode phonecode;
//< audio >
public string audio;
//< call id="" >
[System.Xml.Serialization.XmlAttributeAttribute()]
public long id;
}
//
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallDnis
{
//< number >
public string number;
//< label >
[System.Xml.Serialization.XmlElementAttribute("label")]
public callmeasurementCallDnisLabel[] label;
}
//< label >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallDnisLabel
{ //< label place=" ">
[System.Xml.Serialization.XmlAttributeAttribute()]
public int place;
//< label >
[System.Xml.Serialization.XmlTextAttribute()]
public string value;
}
//< customer >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallCustomer
{
//< number >
public long number;
//< city >
public string city;
//< state >
public string state;
//< postalcode >
public ushort postalcode;
}
//< phonecode >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallPhonecode
{
//< id >
public string id;
//< name >
public string name;
}
#endregion
Hmm the post is replacing values....using (var sReader = new StringReader(reader.ReadToEnd().Replace("&","&")))
Deleteis replacing the second value in the Replace method. It should be: & amp ; (remove the spaces)
XML is :
Delete< rootnode >
< T1 >
< T2 >
< EMPNAME >PARUL< /EMPNAME >
< EMPID >2015< /EMPID >
< T3 >
< T3NAME >NAME1< /T3NAME >
< T3VALUE >VALUE1< /T3VALUE >
< /T3 >
< T3 >
< T3NAME >NAME2< /T3NAME >
< T3VALUE >VALUE2< /T3VALUE >
< /T3 >
< /T2 >
< T1 >
< T2 >
< EMPNAME >PARUL2< /EMPNAME >
< EMPID >20152< /EMPID >
< T3 >
< T3NAME >NAME12< /T3NAME >
< T3VALUE >VALUE12< /T3VALUE >
< /T3 >
< T3 >
< T3NAME >NAME22< /T3NAME >
< T3VALUE >VALUE22< /T3VALUE >
< /T3 >
< /T2 >
< /T1 >
< /ROOTNODE >
using System;
Deleteusing System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void CreateNewOutputRows()
{
//Set Webservice URL
string wUrl = “web api address”
try
{
//Call getWebServiceResult to return our DailyStat array
DataSet outPutStats = getWebServiceResult(wUrl);
//For each output
foreach (var stats in outPutStats.ChannelArray)
{
//For each group of metrics output records
foreach (var records in stats.ItemArray)
{
Output0Buffer.AddRow();
Output0Buffer.empname = records.empname;
Output0Buffer.empid = records.empid;
;
if (records.cust.t3name == "cust name")
{
Output0Buffer.custname = records.cust.t3value;
}
}
}
}
catch (Exception e)
{
failComponent(e.ToString());
}
}
//Method to return our DailyStat array
private DataSet getWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
DataSet xmlResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
//Set xmlString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our XML
XmlSerializer sr = new XmlSerializer(typeof(DataSet));
xmlResponse = (DataSet)sr.Deserialize(reader);
reader.Close();
}
}
//Output connection error message
else
{
failComponent(httpWResp.StatusCode.ToString());
}
}
//Output xml parsing error
catch (Exception e)
{
failComponent(e.ToString());
}
return xmlResponse;
}
//Outputs error message
private void failComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
}
//Class to hold our array of
[XmlRootAttribute("rootnode")]
public class DataSet
{
[XmlElement("t1")]
public t1[] t1Array { get; set;}
}
public class t1
{
[XmlElement("t2")]
public t2[] t2Array { get; set; }
}
public class t3
{
[XmlElement("t3")]
public string t3name { get; set; }
public string t3value { get; set; }
}
public class t2
{
public string empname { get; set; }
public string empid { get; set; }
public t3 cust { get; set; }
}
Hi
ReplyDeleteCan you help me out on this :
XML is :
PARUL
2015
NAME1
VALUE1
NAME2
VALUE2
PARUL2
20152
NAME12
VALUE12
NAME22
VALUE22
"Hi
ReplyDeleteCan you help me out on this :
XML is :
PARUL
2015
NAME1
VALUE1
NAME2
VALUE2
PARUL2
20152
NAME12
VALUE12
NAME22
VALUE22
"
basically it is nested xml
ReplyDelete"Hi
ReplyDeleteCan you help me out on this :
XML is :
then
inside it
thenPARUL
then 2015
then
then NAME1
then VALUE1
then
NAME2
VALUE2
PARUL2
20152
NAME12
VALUE12
NAME22
VALUE22
"
i am not able to paste my xml here
ReplyDeletePut a space between the chevrons and the data i.e. < NAME2 > . Also what have you tried so far?
DeleteXML is :
Delete< rootnode >
< T1 >
< T2 >
< EMPNAME >PARUL< /EMPNAME >
< EMPID >2015< /EMPID >
< T3 >
< T3NAME >NAME1< /T3NAME >
< T3VALUE >VALUE1< /T3VALUE >
< /T3 >
< T3 >
< T3NAME >NAME2< /T3NAME >
< T3VALUE >VALUE2< /T3VALUE >
< /T3 >
< /T2 >
< T1 >
< T2 >
< EMPNAME >PARUL2< /EMPNAME >
< EMPID >20152< /EMPID >
< T3 >
< T3NAME >NAME12< /T3NAME >
< T3VALUE >VALUE12< /T3VALUE >
< /T3 >
< T3 >
< T3NAME >NAME22< /T3NAME >
< T3VALUE >VALUE22< /T3VALUE >
< /T3 >
< /T2 >
< /T1 >
< /ROOTNODE >
Can you help me out on this . i am new to xml
Deletecan you pls help me out
Deleteusing System;
Deleteusing System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void CreateNewOutputRows()
{
//Set Webservice URL
string wUrl = “web api address”
try
{
//Call getWebServiceResult to return our DailyStat array
DataSet outPutStats = getWebServiceResult(wUrl);
//For each output
foreach (var stats in outPutStats.ChannelArray)
{
//For each group of metrics output records
foreach (var records in stats.ItemArray)
{
Output0Buffer.AddRow();
Output0Buffer.empname = records.empname;
Output0Buffer.empid = records.empid;
;
if (records.cust.t3name == "cust name")
{
Output0Buffer.custname = records.cust.t3value;
}
}
}
}
catch (Exception e)
{
failComponent(e.ToString());
}
}
//Method to return our DailyStat array
private DataSet getWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
DataSet xmlResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
//Set xmlString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our XML
XmlSerializer sr = new XmlSerializer(typeof(DataSet));
xmlResponse = (DataSet)sr.Deserialize(reader);
reader.Close();
}
}
//Output connection error message
else
{
failComponent(httpWResp.StatusCode.ToString());
}
}
//Output xml parsing error
catch (Exception e)
{
failComponent(e.ToString());
}
return xmlResponse;
}
//Outputs error message
private void failComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
}
//Class to hold our array of
[XmlRootAttribute("rootnode")]
public class DataSet
{
[XmlElement("t1")]
public t1[] t1Array { get; set;}
}
public class t1
{
[XmlElement("t2")]
public t2[] t2Array { get; set; }
}
public class t3
{
[XmlElement("t3")]
public string t3name { get; set; }
public string t3value { get; set; }
}
public class t2
{
public string empname { get; set; }
public string empid { get; set; }
public t3 cust { get; set; }
}
This comment has been removed by the author.
DeleteHi Jim,
DeleteI have posted the code and XML . Can you look into it and help me out
This comment has been removed by the author.
Deletei am getting null reference error for t3
Deletei am waiting for your reply....
DeleteCan you guide if the classes created re correct or not??
Woah there buddy...I do have a day job you know...
DeleteHere's how I got this to work. I created 2 outputs on the inputs and outputs screen:
Output 0
empname DT_STR 50
empid DT_I4
Customer
custname DT_STR 50
C# code is as follows:
#region Namespaces
Deleteusing System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Xml.Serialization;
using System.Net;
using System.IO;
#endregion
#region Class
/// < summary >
/// This is the class to which to add your code. Do not change the name, attributes, or parent
/// of this class.
///
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Methods
/// < summary >Outputs records to the output buffer
public override void CreateNewOutputRows()
{
//Set Webservice URL
string wUrl = "http://yourwebserviceurl/Respiratory/ResService.asmx/getAgentMetricsXml?offerDate=";
try
{
//Call getWebServiceResult to return our RootObject object
ROOTNODE outPutRootObject = GetWebServiceResult(wUrl);
if (outPutRootObject != null)
{
foreach (ROOTNODET1 T1 in outPutRootObject.T1)
{
Output0Buffer.AddRow();
Output0Buffer.empid = T1.T2.EMPID;
Output0Buffer.empname = T1.T2.EMPNAME;
foreach (ROOTNODET1T2T3 T3 in T1.T2.T3)
{
if (T3.T3NAME == "cust name")
{
CustomerBuffer.AddRow();
CustomerBuffer.custname = T3.T3NAME;
}
}
}
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
/// < summary >
Delete/// Method to call the web service and return our ROOTNODE
///
/// < param name="wUrl" >The web service URL< /param >
/// < returns >The ROOTNODE which contains the deserialized XML< /returns >
private ROOTNODE GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
ROOTNODE xmlResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
//Set xmlString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our XML
XmlSerializer sr = new XmlSerializer(typeof(ROOTNODE));
xmlResponse = (ROOTNODE)sr.Deserialize(reader);
reader.Close();
}
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output xml parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return xmlResponse;
}
/// < summary >
/// Outputs an error message
/// < /summary >
/// < param name="errorMsg" >The error message< /param >
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
#endregion
}
#endregion
#region XML Classes
Delete/// < remarks />
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ROOTNODE
{
private ROOTNODET1[] t1Field;
/// < remarks />
[System.Xml.Serialization.XmlElementAttribute("T1")]
public ROOTNODET1[] T1
{
get
{
return this.t1Field;
}
set
{
this.t1Field = value;
}
}
}
/// < remarks />
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ROOTNODET1
{
private ROOTNODET1T2 t2Field;
/// < remarks />
public ROOTNODET1T2 T2
{
get
{
return this.t2Field;
}
set
{
this.t2Field = value;
}
}
}
/// < remarks />
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ROOTNODET1T2
{
private string eMPNAMEField;
private int eMPIDField;
private ROOTNODET1T2T3[] t3Field;
/// < remarks />
public string EMPNAME
{
get
{
return this.eMPNAMEField;
}
set
{
this.eMPNAMEField = value;
}
}
/// < remarks />
public int EMPID
{
get
{
return this.eMPIDField;
}
set
{
this.eMPIDField = value;
}
}
/// < remarks />
[System.Xml.Serialization.XmlElementAttribute("T3")]
public ROOTNODET1T2T3[] T3
{
get
{
return this.t3Field;
}
set
{
this.t3Field = value;
}
}
}
/// < remarks />
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ROOTNODET1T2T3
{
private string t3NAMEField;
private string t3VALUEField;
/// < remarks />
public string T3NAME
{
get
{
return this.t3NAMEField;
}
set
{
this.t3NAMEField = value;
}
}
/// < remarks />
public string T3VALUE
{
get
{
return this.t3VALUEField;
}
set
{
this.t3VALUEField = value;
}
}
}
#endregion
And Just an FYI...I think the XML you posted is incorrect. I think you meant this:
Delete< ROOTNODE >
< T1 >
< T2 >
< EMPNAME > PARUL< /EMPNAME >
< EMPID > 2015< /EMPID >
< T3 >
< T3NAME > NAME1< /T3NAME >
< T3VALUE > VALUE1< /T3VALUE >
< /T3 >
< T3 >
< T3NAME > NAME2< /T3NAME >
< T3VALUE > VALUE2< /T3VALUE >
< /T3 >
< /T2 >
< /T1 >
< T1 >
< T2 >
< EMPNAME > PARUL2< /EMPNAME >
< EMPID > 20152< /EMPID >
< T3 >
< T3NAME > NAME12< /T3NAME >
< T3VALUE > VALUE12< /T3VALUE >
< /T3 >
< T3 >
< T3NAME > NAME22< /T3NAME >
< T3VALUE > VALUE22< /T3VALUE >
< /T3 >
< /T2 >
< /T1 >
< /ROOTNODE >
Hi Jim,
DeleteThanks for your help.
But the xml that i posted was correct.
Hi Jim.
DeleteThanks for the support. I modified my earlier code.
Ur site and codes are really awesome.
Hi Jim-
ReplyDeleteTrying to make the Call an array. Here are my changes to the XML classes.
#region XML Classes
//< callmeasurement >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
// Begin orig
// public partial class callmeasurement
//{
// public callmeasurementCall call;
//}
// end orig
//begin new
public partial class callmeasurement
{
// private callmeasurementCall[] callField;
///
[System.Xml.Serialization.XmlElementAttribute("call")]
public callmeasurementCall[] call;
//{
// get
// {
// return this.callField;
// }
// set
// {
// this.callField = value;
// }
//}
}
//end new
//< call >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCall
{
//< site_id >
public int site_id;
//< date >
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime date;
//< time >
public string time;
//< time_to_answer >
public string time_to_answer;
//< duration >
public string duration;
//< billed_minutes >
public string billed_minutes;
//< disposition >
public string disposition;
//< ring_to >
public string ring_to;
//< dnis >
public callmeasurementCallDnis dnis;
//< customer >
public callmeasurementCallCustomer customer;
//
public callmeasurementCallPhonecode phonecode;
//< audio >
public string audio;
//< call id="" >
[System.Xml.Serialization.XmlAttributeAttribute()]
public long id;
}
//
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallDnis
{
//< dnis_number >
public string number;
//< label >
[System.Xml.Serialization.XmlElementAttribute("label")]
public callmeasurementCallDnisLabel[] label;
}
//< label >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallDnisLabel
{ //< label place="">
[System.Xml.Serialization.XmlAttributeAttribute()]
public int place;
//< label >
[System.Xml.Serialization.XmlTextAttribute()]
public string value;
}
//< customer >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallCustomer
{
//< number >
public string number;
//< city >
public string city;
//< state >
public string state;
//< postalcode >
public string postalcode;
}
//< phonecode >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class callmeasurementCallPhonecode
{
//< id >
public string id;
//< name >
public string name;
}
#endregion
Here is where I have added a foreach call change to the method..
Deletetry
{
//Call getWebServiceResult to return our callmeasurement object
callmeasurement outPutCallMeasurement = GetWebServiceResult(wUrl);
foreach (var call in outPutCallMeasurement.call)
{
if (outPutCallMeasurement != null)
{
callmeasurementBuffer.AddRow();
callmeasurementBuffer.callid = outPutCallMeasurement.call.id;
callmeasurementBuffer.siteid = outPutCallMeasurement.call.site_id;
callmeasurementBuffer.calldate = outPutCallMeasurement.call.date;
callmeasurementBuffer.calltime = outPutCallMeasurement.call.time;
callmeasurementBuffer.timetoanswer = outPutCallMeasurement.call.time_to_answer;
callmeasurementBuffer.duration = outPutCallMeasurement.call.duration;
callmeasurementBuffer.billedminutes = outPutCallMeasurement.call.billed_minutes;
callmeasurementBuffer.disposition = outPutCallMeasurement.call.disposition;
callmeasurementBuffer.ringto = outPutCallMeasurement.call.ring_to;
callmeasurementBuffer.dnisnumber = outPutCallMeasurement.call.dnis.number;
callmeasurementBuffer.customerphone = outPutCallMeasurement.call.customer.number;
callmeasurementBuffer.customercity = outPutCallMeasurement.call.customer.city;
callmeasurementBuffer.customerpostalcode = outPutCallMeasurement.call.customer.postalcode;
callmeasurementBuffer.phonecodeid = outPutCallMeasurement.call.phonecode.id;
callmeasurementBuffer.phonecodename = outPutCallMeasurement.call.phonecode.name;
callmeasurementBuffer.audio = outPutCallMeasurement.call.audio;
foreach (callmeasurementCallDnisLabel label in outPutCallMeasurement.call.dnis.label)
{
labelBuffer.AddRow();
labelBuffer.callid = outPutCallMeasurement.call.id;
labelBuffer.dnisnumber = outPutCallMeasurement.call.dnis.number;
labelBuffer.labelplace = label.place;
labelBuffer.labelvalue = label.value;
}
}
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
After adding the new foreach, I'm getting the following error for each element that I'm trying to deserialize... 'System.Array' does not contain a definition for 'id' and no extension method 'id' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
FYI the change to the XML classes is at the start and marked by "begin/end orig" and "begin/end new"
DeleteTry This:
Deletetry
{
//Call getWebServiceResult to return our callmeasurement object
callmeasurement outPutCallMeasurement = GetWebServiceResult(wUrl);
foreach (callmeasurementCall call in outPutCallMeasurement.call)
{
if (outPutCallMeasurement != null)
{
callmeasurementBuffer.AddRow();
callmeasurementBuffer.callid = call.id;
callmeasurementBuffer.siteid = call.site_id;
callmeasurementBuffer.calldate = call.date;
callmeasurementBuffer.calltime = call.time;
callmeasurementBuffer.timetoanswer = call.time_to_answer;
callmeasurementBuffer.duration = call.duration;
callmeasurementBuffer.billedminutes = call.billed_minutes;
callmeasurementBuffer.disposition = call.disposition;
callmeasurementBuffer.ringto = call.ring_to;
callmeasurementBuffer.dnisnumber = call.dnis.number;
callmeasurementBuffer.customerphone = call.customer.number;
callmeasurementBuffer.customercity = call.customer.city;
callmeasurementBuffer.customerpostalcode = call.customer.postalcode;
callmeasurementBuffer.phonecodeid = call.phonecode.id;
callmeasurementBuffer.phonecodename = call.phonecode.name;
callmeasurementBuffer.audio = call.audio;
foreach (callmeasurementCallDnisLabel label in call.dnis.label)
{
labelBuffer.AddRow();
labelBuffer.callid = call.id;
labelBuffer.dnisnumber = call.dnis.number;
labelBuffer.labelplace = label.place;
labelBuffer.labelvalue = label.value;
}
}
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
Hi Jim,
ReplyDeleteCan you please suggest me some books from where i can learn advanced c# particularly related to consuming web services like the code u posted because i don't find such stuff in books.
Appreciate your help.
This comment has been removed by the author.
ReplyDeleteHi Jim,
ReplyDeleteI need your help with the script. I keep getting unauthorized error as the xml requires me to pass in username and password basic authentication.
I am pasting the code below. Any help from your will be highly appreciated. I am stuck on this for a week now.
This comment has been removed by the author.
ReplyDelete#region Namespaces
Deleteusing System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Xml.Serialization;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Text;
#endregion
#region Class
///
/// This is the class to which to add your code. Do not change the name, attributes, or parent
/// of this class.
///
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Methods
/// Outputs records to the output buffer
public override void CreateNewOutputRows()
{
//Get SSIS Variables
string offerDate = Variables.offerDate;
//Set Webservice URL
string wUrl = "https://service.aljazeera.net/ast_contract.do?XML&https://service.aljazeera.net/ast_contract.do?XML&sysparm_query=sys_updated_onRELATIVEGE@dayofweek@ago@7" + offerDate;
try
{
//Call getWebServiceResult to return our DailyStat array
Ast_contracts outPutStats = GetWebServiceResult(wUrl);
foreach (var stats in outPutStats.MAst_contract)
ReplyDelete{
Output0Buffer.AddRow();
Output0Buffer.active = stats.active;
Output0Buffer.applicationmodel = stats.application_model;
Output0Buffer.approvalhistory = stats.approval_history;
Output0Buffer.approver = stats.approver;
Output0Buffer.commitment = stats.commitment;
}
ReplyDelete}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
private Ast_contracts GetWebServiceResult(string wUrl)
ReplyDelete{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
Ast_contracts xmlResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
//Set xmlString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our XML
XmlSerializer sr = new XmlSerializer(typeof(Ast_contracts));
xmlResponse = (Ast_contracts)sr.Deserialize(reader);
reader.Close();
}
}
else
ReplyDelete{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output xml parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return xmlResponse;
}
private void FailComponent(string errorMsg)
ReplyDelete{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
#endregion
}
#endregion
#region XML Classes
//Class to hold elements within
///[XmlRoot(ElementName = "ast_contract")]
public class Ast_contract
{
public string active { get; set; }
public string application_model { get; set; }
public string approval_history { get; set; }
public string approver { get; set; }
public string commitment { get; set; }
}
[XmlRootAttribute( "xml")]
public class Ast_contracts
{
[XmlElement("Ast_contract")]
public Ast_contract[] MAst_contract { get; set; }
}
#endregion
Please help me with this. I am stuck on this for a week and really need to have this moving to finish my project.
DeleteHey man sorry for the late response. Blogger stopped emailing me comment notices. Still need help?
Delete