• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

Change C# code to VB

Hi,

I have the following code that I need to run from asp.NET but I don't know C# very well and am having trouble figuring out how to convert it to vb.  I haven't had much to do with XML parsing before, so I'm a little lost.  Sorry about the points, it's probably worth 500 for this, but it's all I have left.




<script runat="server">
   void Page_Load(Object Sender, EventArgs e )
   {

      GetASPSessionData( Request, Session, "MaterXML.asp" );
      string strSessionData = "";
      foreach( string key in Session.Keys )
      {
         strSessionData += key + "=" + Session[key].ToString() + "<br>";
      }      

      lblMessage.Text = strSessionData;
   }


   private string RetrieveXML(string XMLSourcePage, System.Web.HttpRequest request )
   {
      /**********************************************
      * Send the request
      **********************************************/

      System.Net.HttpWebRequest HttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://" + XMLSourcePage );
        HttpWebRequest.Headers.Add( "Cookie: " + request.Headers["Cookie"] );
        System.Net.HttpWebResponse HttpWebResponse = (System.Net.HttpWebResponse)HttpWebRequest.GetResponse();

        /**********************************************
        * Receive the return data
        **********************************************/

        System.IO.StreamReader reader = new System.IO.StreamReader(HttpWebResponse.GetResponseStream());

        string xml = "";
        xml = reader.ReadToEnd();
        HttpWebResponse.Close();

        return xml;
   }

   public void GetASPSessionData(System.Web.HttpRequest request, System.Web.SessionState.HttpSessionState session, string XMLPage)
   {
      //Clear the session contents to have a clean session - Optional
      //session.RemoveAll();

      //Define the URL and page to load the Session XML from
      string XMLServer = request.ServerVariables["SERVER_NAME"];
      string XMLVirtualDirectory = request.ServerVariables["SCRIPT_NAME"];
      XMLVirtualDirectory = XMLVirtualDirectory.Substring( 0, XMLVirtualDirectory.LastIndexOf("/") + 1 );

      //Define an XMLDocument to allow easy XML tree navigation
      System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

      string strXML = RetrieveXML(XMLServer + XMLVirtualDirectory + XMLPage, request );

      //Load the document from the reader
      doc.LoadXml(strXML);

      //Loop through the Session element's child nodes and set each Session object
      foreach(System.Xml.XmlNode node in doc.FirstChild.NextSibling.ChildNodes)
      {
         session[node.Name.ToString()] = node.InnerText.ToString();
      }
   }

   
</script>
0
Drizzt95
Asked:
Drizzt95
2 Solutions
 
sridonCommented:
You can use some online convertors like

http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

to convert your code. They are pretty reliable.

Thanks
Sridon
0
 
kGeniusCommented:
<script runat="server">
   private Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs)
      GetASPSessionData(Request, Session, "MaterXML.asp")
      Dim strSessionData As String = ""

      For Each key As String In Session.Keys      
         strSessionData = strSessionData & key & "=" & Session(key).ToString & "<br>"
      Next
      lblMessage.Text = strSessionData
   End Sub

   Private Function RetrieveXML(ByVal XMLSourcePage As String, ByVal request As System.Web.HttpRequest) As String
     '**********************************************
     ' Send the request
     '*********************************************
     Dim HttpWebRequest As System.Net.HttpWebRequest = Ctype(System.Net.WebRequest.Create("http://" + XMLSourcePage), System.NetHttpWebRequest)

     HttpWebRequest.Headers.Add( "Cookie: " & request.Headers("Cookie") )
     
     Dim HttpWebResponse As System.Net.HttpWebResponse= CType(HttpWebRequest.GetResponse), System.Net.HttpWebResponse)

        '**********************************************
        ' Receive the return data
        '*********************************************

        Dim reader As New System.IO.StreamReader(HttpWebResponse.GetResponseStream)GetResponseStream());

        Dim xml  As String= ""
        xml = reader.ReadToEnd()
        HttpWebResponse.Close()

        return xml
   End Function

   Public SubGetASPSessionData(Byval request As System.Web.HttpRequest , ByVal session As System.Web.SessionState.HttpSessionState , ByVal XMLPage As String )
      'Clear the session contents to have a clean session - Optional
      'session.RemoveAll();

      'Define the URL and page to load the Session XML from
      Dim XMLServer As String= Request.ServerVariables("SERVER_NAME")
      Dim XMLVirtualDirectory As String= Request.ServerVariables("SCRIPT_NAME")
      XMLVirtualDirectory = XMLVirtualDirectory.Substring( 0, XMLVirtualDirectory.LastIndexOf("/") + 1 )

      'Define an XMLDocument to allow easy XML tree navigation
      Dim doc As New System.Xml.XmlDocument

      Dim strXML As String= RetrieveXML(XMLServer & XMLVirtualDirectory & XMLPage, request )

      'Load the document from the reader
      doc.LoadXml(strXML);

      'Loop through the Session element's child nodes and set each Session object
      For Each node As System.Xml.XmlNode In doc.FirstChild.NextSibling.ChildNodes
         session(node.Name.ToString) = node.InnerText.ToString
      Next
   End Sub
</script>



I think this should  be it. Haven't tested it though.
Let me know...

Chears,
kGenius
0
 
Drizzt95Author Commented:
Thanks very much for the help from both of you.  That's a nice site to for me to remember Sridon.  I will give a few point to kGenius seeing as though he did put the whole lot in for me, it did make it easier, but I'll give the most to Sridon as he did get in first and that site may have been enough.

Thanks again,
Ian
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now