Geeks With Blogs

News


Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done

How To:

Create webRequest
set webRequest.Proxy
fetch data from vendor via webResponse = webRequest.GetResponse();
use XSD to validate webResponse
use XSD to create strongly-typed dataset
strongly-typed dataset.ReadXML to populate strongly-typed dataset from webResponse

Hopefully a vendor can send you some xml that looks like this:


http://www.SomeVendor.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.SomeVendor.com/mySchema.xsd">
 
     
         LX010053
         2006-09-26
     
     
         LX010063
         2006-09-26
     
     
         LX045490
         2006-09-26
     
 

hopefully the vendor will provide the XSD schema as was done above:

http://www.SomeVendor.com/mySchema.xsd

paste the url into your web browser and see something like this in your browser :



http://www.SomeVendor.com" xmlns:mstns="http://www.SomeVendor.com" xmlns="http://www.SomeVendor.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
 
   
     
       
       
       
       
       
       
     
   

 

 
   
 

 
   
     
       
         
           
             
               
                 
                   
                   
                 

                 
               

             

           

           
           
           
         

       

     

     
     
   

 

save the xsd in a file with an xsd extension in your Visual Studio .NET project

highlight the file in your Visual Studio .NET Solution Explorer,
and change the CustomTool property from to 'MSDataSetGenerator'

double-click the file in your Visual Studio .NET Solution Explorer,
and change the dataset.name property to something useful

re-use the class below to
Create webRequest
set webRequest.Proxy
fetch data from vendor via  webResponse = webRequest.GetResponse();
use XSD to validate webResponse
use XSD to create strongly-typed dataset
strongly-typed dataset.ReadXML to populate strongly-typed dataset from webResponse

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Configuration;
using System.Data;
namespace recorditBusiness
{
    public class records
    {
        private StringBuilder _sbErrors = new StringBuilder();
        public string _ErrorMessage
        {
            get
            {
                return _sbErrors.ToString();
            }
        }
        private string _xmlResponse = "";
        public string _XmlResponse
        {
            get
            {
                return _xmlResponse;
            }
        }
        public bool Getrecords()
        {
            bool myReturnBool = true;
            try
            {
                //--------------------------------------------------------------------------------------------
                //Create webRequest
                //--------------------------------------------------------------------------------------------
                //string recorditrecordsURL = "
https://stage.SomeVendor.com/lx_stage/SomeVendorrecords.xml?LEGALENTITY=cit&USERNAME=cituser&PASSWORD=goSomeVendor&RELATIVEVERSION=-3";
                string recorditrecordsURL = System.Configuration.ConfigurationManager.AppSettings["recorditrecordsURL"];
                string recorditLegalEntity = System.Configuration.ConfigurationManager.AppSettings["recorditLegalEntity"];
                string recorditUsername = System.Configuration.ConfigurationManager.AppSettings["recorditUsername"];
                string recorditPassword = System.Configuration.ConfigurationManager.AppSettings["recorditPassword"];

                recorditrecordsURL = recorditrecordsURL + "?" + recorditLegalEntity + "&" + recorditUsername + "&" + recorditPassword;
                System.Net.WebRequest webRequest = System.Net.WebRequest.Create(recorditrecordsURL);

                //--------------------------------------------------------------------------------------------
                // how to get your proxy from Internet Explorer
                //      open Internet Explorer
                //      Menu:  Tools -> Internet Options
                //      Connections Tab -> Lan Settings Button
                //      copy and paste from "Address" text box e.g. < proxyServer.com >
                //--------------------------------------------------------------------------------------------
                //string internetProxyServer = "cit_fw1.cit.com";
                string internetProxyServer = System.Configuration.ConfigurationManager.AppSettings["internetProxyServer"];

                //--------------------------------------------------------------------------------------------
                //set webRequest.Proxy
                //--------------------------------------------------------------------------------------------
                webRequest.Proxy = new System.Net.WebProxy(internetProxyServer, true);
//webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;

 

 

 

webRequest.Proxy.Credentials =

 

                //--------------------------------------------------------------------------------------------
                //get streamReader via webResponse.GetResponseStream();
                //--------------------------------------------------------------------------------------------
                System.IO.StreamReader streamReader = new System.IO.StreamReader(webResponse.GetResponseStream());

                //--------------------------------------------------------------------------------------------
                //set property (and use up the forward-only streamReader)
                //--------------------------------------------------------------------------------------------
                _xmlResponse = streamReader.ReadToEnd();


                //--------------------------------------------------------------------------------------------
                //--------------------------------------------------------------------------------------------
                // ***   Validate the XML contained in streamReader
                //--------------------------------------------------------------------------------------------
                //--------------------------------------------------------------------------------------------

                //--------------------------------------------------------------------------------------------
                // Create the XmlSchemaSet class.
                //--------------------------------------------------------------------------------------------
                XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();

                //--------------------------------------------------------------------------------------------
                // Add the schema to the collection.
                //--------------------------------------------------------------------------------------------

                //string recorditTargetNamespace = "http://www.SomeVendor.com"; (must match targetNamespace in XML)
                string recorditTargetNamespace = System.Configuration.ConfigurationManager.AppSettings["recorditTargetNamespace"];

                //string recorditrecordsSchema = "C:\aarv\clients\CIT\DotNetProjects\LoanIQ\recorditBusiness\mySchema.xsd";
                string recorditrecordsSchema = System.Configuration.ConfigurationManager.AppSettings["recorditrecordsSchema"];
               
                //xmlSchemaSet.Add(recorditTargetNamespace, recorditTargetNamespace);
                xmlSchemaSet.Add(recorditTargetNamespace, new XmlTextReader(recorditrecordsSchema));


                //--------------------------------------------------------------------------------------------
                // Create the xmlReaderSettings class.
                //--------------------------------------------------------------------------------------------
                XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                xmlReaderSettings.ValidationType = ValidationType.Schema;
                xmlReaderSettings.Schemas = xmlSchemaSet;
                xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);


                //--------------------------------------------------------------------------------------------
                //build stringReader from string
                //--------------------------------------------------------------------------------------------
                System.IO.StringReader stringReader = new System.IO.StringReader(_xmlResponse);

                //--------------------------------------------------------------------------------------------
                // Create the XmlReader class with xmlReaderSettings
                //--------------------------------------------------------------------------------------------
                //XmlReader xmlReader = XmlReader.Create(streamReader, xmlReaderSettings);
                XmlReader xmlReader = XmlReader.Create(stringReader, xmlReaderSettings);

                //--------------------------------------------------------------------------------------------
                // Parse the XML file (streamReader).
                //--------------------------------------------------------------------------------------------
                while (xmlReader.Read())
                {
                    try
                    {
                    }
                    catch (Exception exValidation)
                    {
                        myReturnBool = false;
                        _sbErrors.Append(exValidation.Message);
                    }
                };

                //--------------------------------------------------------------------------------------------
                //--------------------------------------------------------------------------------------------
                // ***   convert XML contained in streamReader to dataset
                //--------------------------------------------------------------------------------------------
                //--------------------------------------------------------------------------------------------

                //--------------------------------------------------------------------------------------------
                // reload stringReader from string
                //--------------------------------------------------------------------------------------------
                stringReader = new System.IO.StringReader(_xmlResponse);

                //--------------------------------------------------------------------------------------------
                // load strong dataset from stringReader
                //--------------------------------------------------------------------------------------------
                dsrecords dsResponseStrong = new dsrecords();
                dsResponseStrong.ReadXml(stringReader);

              
                //--------------------------------------------------------------------------------------------
                // done with webResponse
                //--------------------------------------------------------------------------------------------
                webResponse.Close();
            }
            catch (Exception ex)
            {
                myReturnBool = false;
                string x = ex.ToString();
                System.Diagnostics.Debug.WriteLine(x);
                _sbErrors.Append(x);
                //throw ex;
            }
            finally
            {

            }
            return myReturnBool;
        }

        // Display any validation errors.
        private static void ValidationCallBack(object sender, ValidationEventArgs e)
        {
            throw new Exception("\r\nValidation Error:  " + e.Message);
        }
    }
}

string ProxyUserName = System.Configuration.ConfigurationManager.AppSettings["ProxyUserName"];string ProxyPassword = System.Configuration.ConfigurationManager.AppSettings["ProxyPassword"];string ProxyDomain = System.Configuration.ConfigurationManager.AppSettings["ProxyDomain"];new System.Net.NetworkCredential(ProxyUserName, ProxyPassword, ProxyDomain);
                //--------------------------------------------------------------------------------------------
                //get webResponse via webRequest.GetResponse();
                //--------------------------------------------------------------------------------------------
                System.Net.WebResponse webResponse = webRequest.GetResponse();

 

Posted on Tuesday, September 26, 2006 10:34 AM XML & Datasets | Back to top


Comments on this post: webRequest, webRequest.Proxy, webResponse , XSD to validate webResponse , XSD to create and populate strongly-typed dataset

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net