Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileStream and MemoryStream Conversion from C# to VB.NET

Status
Not open for further replies.

onedizzydevil

Programmer
Mar 24, 2001
103
US
I am trying to convert the following C# code:

Code:
private Stream GetXmlTemplate() {
	if (this.Cache.Get(this.XmlTemplate) != null) {
		MemoryStream ms = (MemoryStream)this.Cache.Get(this.XmlTemplate);
		ms.Position = 0;
		return CopyStream(ms);
	} else {
		FileStream fs = null;
		try {
			fs = new FileStream(this.XmlTemplate,FileMode.Open,FileAccess.Read);
			MemoryStream ms = new MemoryStream();
			byte[] bytes = new byte[1024];
			int i;
			while ((i = fs.Read(bytes,0,bytes.Length)) != 0) {
				ms.Write(bytes,0,i);
			}
			ms.Position = 0;
			CacheDependency dep = new CacheDependency(new string[]{this.XmlTemplate});
			this.Cache.Insert(this.XmlTemplate,ms,dep);
			return CopyStream(ms);
		}
		catch (Exception exp) {
			throw new ApplicationException(String.Concat("Unable to cache XML template (",exp.Message,"): ",this.XmlTemplate));
		}
		finally {
			fs.Close();
		}
	}
}



to VB.NET, I used an online converter to receive the following code but it does not work.

Code:
Private Function GetXmlTemplate() As Stream
    If Not Me.Cache.Get(Me.XmlTemplate) Is Nothing Then
        Dim ms As MemoryStream = CType(Me.Cache.Get(Me.XmlTemplate), MemoryStream)
        ms.Position = 0
        Return CopyStream(ms)
    Else
        Dim fs As FileStream = Nothing
        Try
            fs = New FileStream(Me.XmlTemplate, FileMode.Open, FileAccess.Read)
            Dim ms As MemoryStream = New MemoryStream
            Dim bytes(1024) As Byte
            Dim i As Integer
            While (CType(i = fs.Read(bytes, 0, bytes.Length), Integer) > 0)
                ms.Write(bytes, 0, i)
            End While
            ms.Position = 0
            Dim dep As CacheDependency = New CacheDependency(New String() {Me.XmlTemplate})
            Me.Cache.Insert(Me.XmlTemplate, ms, dep)
            Return CopyStream(ms)
         Catch exp As Exception
            Throw New ApplicationException(String.Concat("Unable to cache XML template (", exp.Message, "): ", Me.XmlTemplate))
         Finally
            fs.Close()
         End Try
    End If
End Function



I get the following error:

The root element is missing.



Any assistance would be greatly appreciated.






Wayne Sellars

"Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0."
 
on what line?
Did you import system.io ?
Or could it be that the xml is missing it's root element?

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
It is the following section that is not returning the same results as the C# version.

Code:
While (CType(i = fs.Read(bytes, 0, bytes.Length), Integer) > 0)
                ms.Write(bytes, 0, i)
            End While


The file works fine with the C# version and the file path is the same between the versions.

Wayne Sellars

"Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top