topical media & game development

talk show tell print

mashup-amazon-13-13-05-GoogleAmazon-App-Code-WebUtility.cs / cs



  
Author: Francis A. Shanahan

www.FrancisShanahan.com

using System; using System.Xml; using System.Net; using System.IO; using System.Text; using System.Configuration; using System.Xml.Xsl; using System.Collections.Specialized;

Simply Helper class with many useful Mashup functions


public static class webUtility { static webUtility() { }
<summary> Transforms an xml document using an XSL style sheet </summary> <param name="myDoc">the XML to tranform</param> <param name="strXsl">The filename of the XSL file to use</param> <returns>The transformed document as a string</returns> public static string DoXSLTransformation(XmlDocument myDoc, string strXsl) { return DoXSLTransformation(myDoc, strXsl, null); }

       public static string DoXSLTransformation(XmlDocument myDoc, string strXsl, XsltArgumentList myArgs)
      {
          // Create an XSL transformation
          XslCompiledTransform myProcessor = new XslCompiledTransform();
  
          // Load the XSL document
          myProcessor.Load(System.Web.HttpContext.Current.Server.MapPath(strXsl));
  
          // Create a text writer for use in the transformation
          StringWriter myWriter = new StringWriter();
  
          // Transform the source XML document
          myProcessor.Transform(myDoc, myArgs, myWriter);
  
          // Return the result as a string
          return myWriter.ToString();
      }
      
  
  
<summary> Retrieves a Uri using HTTP GET and returns the results as XML </summary> <param name="strURL">Uri to retrive</param> <returns>Xml document results</returns> public static XmlDocument GetUri(string strURI) { // Create a request object HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strURI);

          // Obtain the response from the server
          HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
          Stream myResponseStream = myResponse.GetResponseStream();
  
          // Load the result into an XML document
          XmlDocument myDoc = new XmlDocument();
          myDoc.Load(myResponseStream);
  
          // return the XML document
          return myDoc;
      }
  
      // Retrieves a response from a WebRequest
      // that was made asynchronously
      public static XmlDocument GetUri(HttpWebRequest myRequest, IAsyncResult myResult)
      {
          XmlDocument myDoc;
          HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(myResult);        
          StreamReader myReceiveStream = new StreamReader(myResponse.GetResponseStream());
              
          // Create the doc and load it
          myDoc = new XmlDocument();        
          myDoc.Load(myReceiveStream);
  
          // Return the doc
          if (myDoc != null) { 
              return myDoc;        
          } else {
              return null;
          }
      }
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.