How to read specific data fromJSON array to VB.net string? - vb.net

really need help, i need to make somehow to read from JSON array to VB.net string, specific value , this is my eg of array http://playnet.pro/client-files/gtracker/server.php?game=cssource&ip=216.52.148.47&port=27015
I made stream reader
Dim address As String = "http://playnet.pro/client-files/gtracker/server.php?game=cssource&ip=216.52.148.47&port=27015"
Dim client As WebClient = New WebClient()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
Dim array As String = reader.ReadToEnd
but how to find my values or filter it? Thank you

For parsing JSON in VB.NET, you have option to use any one of the below libraries:
*) Newtonsoft.Json library You can use this from here
Tutorial: https://stackoverflow.com/a/20080586/4360419
*) JSON.NET library You can use this from here
Tutorial: You can also find the tutorial of this from the same side
You can also generate your VB.NET classes by your JSON from the below link
http://jsontodatacontract.azurewebsites.net/

Related

how to read a CSV file as resource using TextFieldParser

I have a CSV file in my project resources which I want to read using FileIO.TextFieldParser
I tried Dim parser = new TextFieldParser(My.Resources.ArticlesCSV), but since TextFieldParser expects either a path (as string) or a stream, this is not working.
I guess one possibility is to convert the resource to a stream, but I cannot find how to do that...
What is the best way to get this working?
You can create a new instance of IO.StringReader which is of type TextReader that TextFieldParser will accept. Just pass your CSV file (Thanks to AndrewMorton)
Using strReader As New IO.StringReader(My.Resources.ArticlesCSV)
Using textparser As New TextFieldParser(strReader)
textparser.Delimiters = {","}
While Not textparser.EndOfData
Dim curRow = textparser.ReadFields()
' Do stuff
End While
End Using
End Using

SvgDocument.Draw() Object reference not set to an instance of an Object

I have looked around Stackoverflow and the internet in generel, but haven't found a post that could help me solve my problem.
My problem is that in the following code snippet at line
Dim bm As Bitmap = SvgDoc.Draw()
I get an Object reference not set to an instance of an object.
Protected Function SvgToPng(ByVal svg As String) As Byte()
svg = svg.Replace("url(""#lineArea"")", "url('#lineArea')")
Dim byteArray = Encoding.UTF8.GetBytes(svg)
Dim str As New MemoryStream(byteArray)
Dim svgDoc = SvgDocument.Open(str)
scaleSvgDoc(svgDoc, 7)
Dim bm As Bitmap = svgDoc.Draw()
Dim out As New MemoryStream
bm.Save(out, ImageFormat.Png)
Return out.ToArray
End Function
I have multiple buttons, under different menus that access this method. My problem is that for a single of these menus, I get the problem as described above, but I don't get it for the rest.
I have checked that both the SvgDoc, str and byteArray all are set, and the only difference between the working one, and one that doesn't work, is the SvgString (in this case svg).
Anyone that can help me here?
EDIT: It's the SVG Rendering Engine library that I use.
I don't know much about this library, I assume you are using SVG Rendering Engine? But I noticed that there is a method SvgDocument.OpenAsBitmap. Why not just open as a bitmap and then change to whatever image format you want?

How do you delete a file generated via webapi after returning the file as response?

I'm creating a file on the fly on a WebAPI call, and sending that file back to the client.
I think I'm misunderstanding flush/close on a FileStream:
Dim path As String = tempFolder & "\" & fileName
Dim result As New HttpResponseMessage(HttpStatusCode.OK)
Dim stream As New FileStream(path, FileMode.Open)
With result
.Content = New StreamContent(stream)
.Content.Headers.ContentDisposition = New Headers.ContentDispositionHeaderValue("attachment")
.Content.Headers.ContentDisposition.FileName = fileName
.Content.Headers.ContentType = New Headers.MediaTypeHeaderValue("application/octet-stream")
.Content.Headers.ContentLength = stream.Length
End With
'stream.Flush()
'stream.Close()
'Directory.Delete(tempFolder, True)
Return result
You can see where I've commented things out above.
Questions:
Does the stream flush/close itself?
How can I delete the tempFolder after returning the result?
On top of all this, it would be great to know how to generate the file and send it to the user without writing it to the file system first. I'm confident this is possible, but I'm not sure how. I'd love to be able to understand how to do this, and solve my current problem.
Update:
I went ahead with accepted answer, and found it to be quite simple:
Dim ReturnStream As MemoryStream = New MemoryStream()
Dim WriteStream As StreamWriter = New StreamWriter(ReturnStream)
With WriteStream
.WriteLine("...")
End With
WriteStream.Flush()
WriteStream.Close()
Dim byteArray As Byte() = ReturnStream.ToArray()
ReturnStream.Flush()
ReturnStream.Close()
Then I was able to stream the content as bytearraycontent:
With result
.Content = New ByteArrayContent(byteArray)
...
End With
On top of all this, it would be great to know how to generate the file and send it to the user without writing it to the file system first. I'm confident this is possible, but I'm not sure how. I'd love to be able to understand how to do this, and solve my current problem.
To do the same thing without writing a file to disk, you might look into the MemoryStream class. As you'd guess, it streams data from memory like the FileStream does from a file. The two main steps would be:
Take your object in memory and instead of writing it to a file, you'd serialize it into a MemoryStream using a BinaryFormatter or other method (see that topic on another StackOverflow Q here: How to convert an object to a byte array in C#).
Pass the MemoryStream to the StreamContent method, exactly the same way you're passing the FileStream now.

saxon9EE - javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet

I am trying to transform our system data (in xml format) to defined output xml file.
To create this output xml, I am planning to use XSLT version 2.0. Hence I'm using Saxon EE evaluation version (SaxonEE9-5-1-2 version)
I am trying to demonstrate this software to my team thereby buy the software and use the features and functionality of XSLT 2.0, XQuery, etc
Currently I am working on a prototype project - where I am having problem.
VB.NET Transformation code is as follows:
Dim strXSLT As String = String.Empty
Dim strXML As String = String.Empty
'Retrieve data from database as string
strXML = GetData()
Dim processor As Saxon.Api.Processor = New Saxon.Api.Processor()
Dim builder As Saxon.Api.DocumentBuilder = processor.NewDocumentBuilder()
'Retrieve XSLT file content from database as string
strXSLT = GetXSLT()
'Convert XSLT string data as memorystream
Dim byteDataXSLT() As Byte
byteDataXSLT = System.Text.Encoding.UTF8.GetBytes(strXSLT)
Dim msXSLT As New System.IO.MemoryStream(byteDataXSLT,0, byteDataXSLT.Length)
'Convert XML string data as memorystream
Dim byteDataXML() As Byte
byteDataXML = System.Text.Encoding.UTF8.GetBytes(strXML)
Dim msXML As New System.IO.MemoryStream(byteDataXML,0, byteDataXML.Length)
'Save the xml file before processing
Dim fsXML As New System.IO.FileStream("C:\Temp\XML.xml", System.IO.FileMode.OpenOrCreate)
Dim byteFileXML() As Byte
byteFileXML = msXML.ToArray()
fsXML.Write(byteFileXML, 0, byteFileXML.Length)
'Save the xsl file before processing
Dim fsXSLT As New System.IO.FileStream("C:\Temp\XSL.xslt", System.IO.FileMode.OpenOrCreate)
Dim byteFileXSLT() As Byte
byteFileXSLT = msXSLT.ToArray()
fsXSLT.Write(byteFileXSLT, 0, byteFileXSLT.Length)
Dim sURI As New Uri("file:///C:/")
builder.BaseUri = sURI
Dim input As Saxon.Api.XdmNode = builder.Build(msXML)
Dim transformer As Saxon.Api.XsltTransformer = processor.NewXsltCompiler().Compile(msXSLT).Load()
transformer.InitialContextNode = input
Dim serializer As New Saxon.Api.Serializer()
serializer.SetOutputFile(strOutputFileName)
transformer.Run(serializer)
I am getting below error on below line, only when I run through program. When I do the transformation manually i am not getting this error.
Dim transformer As Saxon.Api.XsltTransformer = processor.NewXsltCompiler().Compile(msXSLT).Load()
Error Message:
saxon9ee javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet.
But when I saved the xml and xslt memorystream to filestream and do the transformation manually, we get the desired output.
We are trying to figure out the root cause of this issue, but no luck.
Any details on this error message would be really helpful and very much appreciated.
Please let me know if you need more information.
Many Thanks in advance
Raghu
Looks like you are not seeing the error messages produced by the compiler. If you aren't running from the Console, then set the ErrorList property on the XsltCompiler, and when the exception occurs, inspect and display the errors somewhere, e.g. send them to a log file or pop up a window containing them.
Without seeing the error message, I can't begin to guess what the error might be.

Deserialize Object from String with StringReader

I am currently able to Serialize an object and store the data in a database by using the following code:
Dim x As New XmlSerializer(myObject.GetType)
Dim sw As New IO.StringWriter()
x.Serialize(sw, MyObject)
'Write sw to database
I can't figure out how to Deserialize the data from the database into the object.
I've tried using the reverse with no success. (The problem with the code below is that the strReader is null:)
Dim x As New Xml.Serialization.XmlSerializer(GetType(myObject))
Dim strReader As New StringReader(dr("xml_data") & "")
Dim tmpData As myObject = CType(x.Deserialize(strReader), myObject)
Any assistance will be greatly appreciated.
Refer to follow MS KB article that talks about serialization and deserialization.(KB Article).
You code should be fine as per the code. But, sharing the code of your object help in identification other possible issues.