Text File Output

DekaFlash

Well-known member
Joined
Feb 14, 2006
Messages
117
Programming Experience
1-3
Hi,

How do I output non string data to a text file?

Thanks
Boulent
 
That's like asking how you put non-water into a glass of water. If the glass doesn't contain water then it isn't a glass of water. A text file is a file that contains text. If the file doesn't contain text then it's not a text file. You can give any file a TXT extension but that doesn't make it a text file.

Now having said that, all data is just a series of bytes, whether it's text or something else. That means that whatever series of bytes make up some object can be interpreted as a string of characters and written to a text file. To do that you would have to convert the object to a byte array, then pass that to the GetString method of an Encoding object. Treating data as a string when it's not a string can lead to issues though. If you use the wrong type of encoding you may find that data gets altered in the process of reading, writing and converting. For that reason it's generally a bad idea. If you really want to do it then you should read the MSDN documentation for the Encoding class and experiment a bit.
 
Its a custom data type been returned from a web service, I am also trying to if possible log soap errors if and when they occur.

My application is a vb.net 2003 winforms application
 
Last edited:
Logging errors to a text file is pretty normal but the usual method of persisting objects is via serialization to either XML or a binary file. Once you set up the custom serialisation it's just a matter of a couple of lines of code to save or load an object.
 
I've made the class serializable with the following line, I am getting the following error: Attribute specifier is not a complete statement. Use a line continuation to apply the attribute to the following statement.

VB.NET:
Namespace WebService
'
'<remarks/>
<System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Web.Services.WebServiceBindingAttribute(Name:="BFServiceV2", [Namespace]:="http://www.betfair.com/publicapi/BFServiceV2/"), _
System.Xml.Serialization.XmlIncludeAttribute(GetType(APIRequest)), _
System.Xml.Serialization.XmlIncludeAttribute(GetType(APIResponse))> _
 
<Serializable()> PublicClass BFServiceV2

Below is my serialization code, however binaryformatter is not been recognised.

VB.NET:
Dim myFileStream As New FileStream("Logs\LoginLog.txt", FileMode.CreateNew)
Dim MyFormatter As New BinaryFormatter
 
MyFormatter.Serialize(myFileStream, results(0))


 
Last edited by a moderator:
You've got those first five attributes in one set of angled brackets, then a line continuation character, then a blank line, then another attribute in a second set of angled brackets. If that's how your code apperas in the IDE then that will be your issue. At the very least you'd need to remove that blank line. I'd also think that you'd have to only have one set of angled brackets with all attributes contained in it.
 
Thanks, I managed to get it working

VB.NET:
Dim myFileStream AsNew FileStream("Logs\LoginLog.txt", FileMode.CreateNew)
Dim MyFormatter AsNew binaryformatter
MyFormatter.Serialize(myFileStream, CObj(results(0)))
I am now getting an exception saying that my data is not serializable, is there something more I need to do.
 
Last edited by a moderator:
The class have to be marked with the Serializable attribute like you tried to do in earlier posts.
VB.NET:
<Serializable()> Class a
Public b As String
End Class
 
I have included the line Serializable()> Public Class BFServiceV2, however I do not have access to the source of what I am trying to serialize.

It is a web service?

Are there any other solutions?





 
That doesn't make sense, you say you mark the class serializable, then you say you don't have the source?
 
The class I am trying to serialize is a class that is generated when you add a web reference to a vb.net 2003 project.

However when i try to output loginresp using serialization it says it isn't serializable.

Thanks
 
the creator of the web referenced class probably didnt make it serializable, which means you're not going to be able to do it either
 
Back
Top