Geeks With Blogs

News


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

        public bool GetDataFromWebSite()

        {

            bool myReturnBool = true;

            try

            {

               

                System.Net.WebRequest webRequest = System.Net.WebRequest.Create(“www.WhereTheDataIs.com”);

 

                //--------------------------------------------------------------------------------------------

                // 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 ProxyServerName = "cit_fw1.cit.com";

                string ProxyServerName = System.Configuration.ConfigurationManager.AppSettings["ProxyServerName"];

 

                //--------------------------------------------------------------------------------------------

                //set webRequest.Proxy

                //--------------------------------------------------------------------------------------------

                webRequest.Proxy = new System.Net.WebProxy(ProxyServerName, true);

                //webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;

 

                string ProxyUserName = System.Configuration.ConfigurationManager.AppSettings["ProxyUserName"];

                string ProxyPassword = System.Configuration.ConfigurationManager.AppSettings["ProxyPassword"];

                string ProxyDomain = System.Configuration.ConfigurationManager.AppSettings["ProxyDomain"];

                webRequest.Proxy.Credentials = new System.Net.NetworkCredential(ProxyUserName, ProxyPassword, ProxyDomain);

                //--------------------------------------------------------------------------------------------

                //get webResponse via webRequest.GetResponse();

                //--------------------------------------------------------------------------------------------

                System.Net.WebResponse webResponse = webRequest.GetResponse();

 

                //--------------------------------------------------------------------------------------------

                //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 thw XML contained in streamReader

                //--------------------------------------------------------------------------------------------

                //--------------------------------------------------------------------------------------------

 

                //--------------------------------------------------------------------------------------------

                // Create the XmlSchemaSet class.

                //--------------------------------------------------------------------------------------------

                XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();

 

                //--------------------------------------------------------------------------------------------

                // Add the schema to the collection.

                //--------------------------------------------------------------------------------------------

 

                string TargetNamespace = System.Configuration.ConfigurationManager.AppSettings["TargetNamespace"];

 

                //string MySchema = System.Configuration.ConfigurationManager.AppSettings["MySchema"];

               

                //xmlSchemaSet.Add(TargetNamespace, new XmlTextReader(MySchema));

                xmlSchemaSet.Add(TargetNamespace, new XmlTextReader("my.xsd"));

 

 

                //--------------------------------------------------------------------------------------------

                // 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

                //--------------------------------------------------------------------------------------------

                _myDataSet.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;

        }

Posted on Tuesday, July 31, 2007 4:04 AM XML & Datasets | Back to top


Comments on this post: Get and Validate XML from WebSite and convert to Dataset

# re: Get and Validate XML from WebSite and convert to Dataset
Requesting Gravatar...
way to flood the front page
Left by Will Pritchard on Jul 31, 2007 6:22 AM

# re: Get and Validate XML from WebSite and convert to Dataset
Requesting Gravatar...
Copy/paste from http://vyaskn.tripod.com ? why?
Left by Bob on Jul 31, 2007 1:47 PM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net