Sample HTML placed in a variable - vb.net

I can retrieve the contents of a webpage into a variable like:
x = objIE.Document.body.outerHTML
But during development (data parse), I don’t want to keep pulling up a live site and instead just want to store some sample html in a format that can be placed in a variable. The issue is that trying to directly place a raw html sample into a variable creates errors and would require going through and escaping quotes etc. What’s an easy way to put place a large block of sample html in a variable?

You could read the html into a string variable using IO.StreamReader
Dim docStuff As String
Dim strReader As IO.StreamReader
Dim strWriter As IO.StreamWriter
strReader = New IO.StreamReader(Application.StartupPath & "/me.html")
docStuff = strReader.ReadToEnd
strReader.Close()
then write it back out using IO.StreamWriter
strWriter = New IO.StreamWriter(Application.StartupPath & "/changedCode.html")
strWriter.Write(docStuff)
strWriter.Close()
I have never had issues with this method with escape characters, etc. I pull it in, then I can look at the code, make changes, etc. Works like a charm.

Related

VB writeline writes corrupt lines to text file

Is it possible for the following code to produce NUL values within a text file?
var temp_str = "123456;1234567"
My.Computer.FileSystem.WriteAllText(Path & "stats.txt", temp_str, False)
It seems simple, but it writes quite often and I'm seeing several files that get accessed by the application that have Strings written to as:
When opening the file with Notepad++. Some other editors show just squares, and it seems like each character is represented by a block/NUL.
So far I've been unable to reproduce this on my test system. I just find the files on a COMX module's file system that's been running in the field and comes back faulty, but I've been seeing enough of these files to make it a problem that needs to be solved.
Does anyone have an idea to prevent this behaviour?
Hard to say what the problem is without more code, but try this if you want to replace the existing contents of the file:
Dim fileContent = "My UTF-8 file contents"
Using writer As IO.StreamWriter = IO.File.CreateText(fullPathIncludingExtension)
writer.Write(fileContent)
End Using
Or this if you want to append UTF-8 text:
Dim newLines = "My UTF-8 content to append"
Using writer As IO.StreamWriter = IO.File.AppendAllText(fullPathIncludingExtension)
writer.Write(fileContent)
End Using
If you want to append Unicode text, you must use a different constructor for StreamWriter:
Using writer As IO.StreamWriter = New IO.StreamWriter("full/path/to/file.txt", True, Text.Encoding.Unicode)
writer.Write(MyContentToAppend)
End Using
Note that the True argument to the constructor specifies that you want to append text.

Using ParseQueryString in Winform

My program is writing URL-encoded strings into a text file like this one.
topic1=1&topic2=2&topic3=3&
However, I'm trying to reverse the process by opening the same file and breaking that file up in a manner that I can retrieve into name/value-like pairs.
' Final Challenge Category
Dim sr As StreamReader = New StreamReader("./Game" + nudGameNo.Value.ToString() + "/Final/category.txt")
strLine = WebUtility.UrlDecode(sr.ReadLine)
The closest function that I can find to help is HttpUtility.ParseQueryString but I can't seem to run it in a WinForms application. (Even if I use Imports System.Web)
I've also tried to do a .Split with & being the separator, however problems start up if a particular value contains an & of it's own.
Is it possible to break this form of string up?

Range.InsertXML using Transform

I am using Range.InsertXML to add formatted content to an existing MS Word document.
AFAICT, the method .InsertXML takes parameters XML as a string and XLST (Transform) as a variant; however the documentation for .InsertXML is lacking in that it does not provide an example that includes the use of Transform.
In responding to this question: Range.InsertXML throws when inserting Open XML Cindy Meister linked to an excellent resource that explains lotd about Words XML and how to use it, but I still can't find any examples where Transform is used.
What I am trying to do is the same as in this question: XSLT create table with dynamic number of rows and columns (but using VBA).
From that question this is the modified code that I expected to work:
Sub testInsertXML()
Dim strXML As String
Dim XSLT As Variant
strXML = "<Movies><Genre name=""Action""><Movie><Name>Crash</Name><Released>2005</Released></Movie></Genre><Genre name=""Drama""><Movie><Name>The Departed</Name><Released>2006</Released></Movie><Movie><Name>The Pursuit of Happyness</Name><Released>2006</Released></Movie></Genre><Genre name=""Comedy""><Movie><Name>The Bucket List</Name><Released>2007</Released></Movie></Genre></Movies>"
XSLT = "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" xmlns:w=""http://schemas.microsoft.com/office/word/2003/wordml""><xsl:output indent=""yes""/><xsl:template match=""/""><w:document><w:body><w:tbl><w:tr><xsl:for-each select=""/Movies/Genre""><w:tc><w:p><w:r><w:t><xsl:value-of select=""#name""/></w:t></w:r></w:p></w:tc></xsl:for-each></w:tr><!-- Movies? --><xsl:call-template name=""movies-row""><xsl:with-param name=""i"" select=""1""></xsl:with-param></xsl:call-template></w:tbl></w:body></w:document></xsl:template><xsl:template name=""movies-row""><xsl:param name=""i""/><w:tr><xsl:for-each select=""/Movies/Genre""><w:tc><w:p><w:r><w:t><xsl:choose><xsl:when test=""count(Movie) >= $i""><xsl:value-of select=""concat(Movie[$i]/Name, ' (', Movie[$i]/Released, ')')""/></xsl:when><xsl:otherwise><!-- empty cell --></xsl:otherwise></xsl:choose></w:t></w:r></w:p></w:tc></xsl:for-each></w:tr><xsl:if test=""/Movies/Genre[count(Movie) > $i]"">" & _
"<xsl:call-template name=""movies-row""><xsl:with-param name=""i"" select=""$i + 1""/></xsl:call-template></xsl:if></xsl:template></xsl:stylesheet>"
Selection.Range.InsertXML strXML, XSLT
End Sub
I have added double quotes to the strings (so that the quotes appear correctly when using debug.print) AND I have added additional tags as described here.
I think I'm not setting up the Transform correctly. If the Transform is set to vbnullstring everything works. Otherwise I get an error:
XML markup cannot be inserted in the specified location
Basically, as an answer to this question, I'd like a minimal example showing how to use .InsertXML using Transform.
Working solution:
The original source for the xml appears to be from this learn.microsoft.com site. Incidentally, there are also some instructions on how to create an XSLT directly from a MS Word document (well, albeit manually).
At any rate, it's easiest to just to the transform and insert as two separate steps, like this:
Sub xmlTest()
Dim xData As MSXML2.DOMDocument60
Dim xTnsf As MSXML2.DOMDocument60
Dim xOutp As MSXML2.DOMDocument60
Set xData = New MSXML2.DOMDocument60
Set xTnsf = New MSXML2.DOMDocument60
Set xOutp = New MSXML2.DOMDocument60
xData.Load "C:\Temp\MyMovies.xml"
xTnsf.Load "C:\Temp\MyMovies.xslt"
xData.transformNodeToObject xTnsf, xOutp
ThisDocument.Range.InsertXML xOutp.XML
Debug.Print xOutp.XML
End Sub

File is currently in use. Can't write to it

So, i have this problem for a while and it's truly giving me headaches ... I want to download a string from a website, then save it so a file in my computer that i will create on the spot , let's say the file is D:\cars.txt , the file path by the way is Input(3) .
I tried this but it just won't work!
I ran out of ideas, can't find anything to make it work properly.
If Not IO.File.Exists(Input(3)) Then IO.File.Create(Input(3))
Dim str As String = WC.DownloadString(Input(2))
Using wrtr As IO.StreamWriter = New IO.StreamWriter(Input(3))
wrtr.Write(str)
System.Threading.Thread.Sleep(150)
wrtr.Close()
End Using
It won't write to the file because it's still in use, how can i make it work properly :( ?
IO.File.Create(Input(3) creates or overwrites the file and returns a FileStream. From MSDN:
The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.
You can rewrite it as follows,
Dim str As String = WC.DownloadString(Input(2))
System.IO.File.WriteAllText(input(3),str)

VB.net will not read text file correctly

I've been trying to use StreamReader to read a log file. I cannot verify what it is encoded in, as when I open it in notepad++ and select ANSI encoding, I get this result:
I'm getting the characters needed when using ANSI but they are followed by things like [NULL][EOT][SOH][NUL][SI]
When I try and read the file in VB (using StreamReader or ReadAll) with ANSI encoding selected the resulting string I get back is completely wrong.
How could I read a file like this in VB.net?
You could use the IO.File.ReadAllText("File Location", encoding as System.Text.Encoding) method,
Dim textFromFile as string = IO.File.ReadAllText("C:\Users\Jason\Desktop\login20130417.rdb", System.Text.Encoding.ASCII) 'Or Unicode, UFT32, UFT8, UFT7, BigEndianUnicode or default. Default is ANSI.
If you still don't get the text you need by using the default encoding (ANSI), then you can always try the other 6 different encoding methods.
Update...
It appears that your file is corrupt, using the code below I was able to get a binary representation of whatever is in the file, I got this,
1111111111111101000001110000010000000000000001010000000000010011000000000000100000000000000111100000000000100110000000000011100000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111110100000111000001000000000000000101000000000001001100000000000010000000000000011110000000000010100000000000111111111111110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110011100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The massive amount of null data would suggest that the file is corrupt, which would also explain why we are not getting a lot of data whenever we try to read the file.
The code,
Dim fileData As String = IO.File.ReadAllText("C:\Users\Jason\Desktop\login20130417.rdb")
Dim i As Integer = 0
Dim binaryData As String = ""
Dim ch As String = ""
Do Until i = fileData.Length
ch = fileData.Chars(i)
bin = bin & System.Convert.ToString(AscW(ch), 2).PadLeft(8, "0")
i = i + 1
Loop
As #Daniel A. White suggested in his comment, that file does not appear to be encoded like a "normal" text file. A StreamReader will not work in this situation. I would attempt to use a BinaryReader.
Rdb file? Never heard of it. Quick google makes it less clear - n64 database file, Darkbot, etc...
However considering the name you have, and the general look of the opened file, i would say its a binary file.
If you want to read the file in vb.net you'll need a library of sorts, and i can't help you with one until you are able to shed some light on what the file may be, or what it was created with.