Deserialize Object from String with StringReader - vb.net

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.

Related

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 to read specific data fromJSON array to VB.net string?

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/

How to serialize a List in VB?

I am writing a program to take several fields of a "patient" and serialize the data into XML. I am grabbing all the information as follows:
Dim pacients As New List(Of Patients)
Dim p As New Patients
p.mFirstName = txtFirstName.ToString
p.mlastName = txtLastName.ToString
p.mInsurance = txtInsurance.ToString
p.mDOB = txtDateOfBirth.ToString
p.mEmail = txtEmail.ToString
p.mPhone = txtPhone.ToString
p.mPlanID = txtPhone.ToString
p.mSubID = txtSubID.ToString
And this is the part with the big error (this is triggered by a different 'save all' button, where as the code before is part of a 'add to list' button):
Dim writer As New StreamWriter(PatientFileName.PatientFileName)
Dim serial As New XmlSerializer(GetType(Patients))
serial.Serialize(writer, pacients) 'in this line it just says there was an error generating the xml document
writer.Close()
If the patients class is required I will post it.
I found the problem. This:
Dim serial As New XmlSerializer(GetType(Patients))
Is expecting one object of patient, this needs to be changed to expect a list as follows:
Dim serial As New XmlSerializer(GetType(List(Of Patient)))
Thanks for all the suggestions.
This should do it... you can loop through your list and call this method each time, also make sure you have the serialize attribute at the top of your patient class, its required. Also your issue is because your gettype is for your patient class, not a list of patients which your passing in to serialize. Also from the looks of code you posted your don't need a list, just use your class object and save it...
Public Sub SavePatient(Byval patients As Patients)
Dim writer As New System.Xml.Serialization.XmlSerializer(GetType(patients))
Dim file As New System.IO.StreamWriter(
"YOURPATH.xml")
writer.Serialize(file, patients)
file.Close()
End Sub

Set a report datasource instance at run time

I have created a report that is based on a business object - this works great. I am now trying to add a button that renders the report directly to PDF (in a winforms application).
I know what I need to do - in code I am creating a ReportViewer, setting the DataSource, specifying the report (it's an embedded resource), then rendering the report into a byte array before using System.IO.File.WriteAllBytes to flush the byte array to disk. One thing I'm hung up on though, is how do I specify the instance of the object properly? I keep getting the "An error has occurred during the report processing" error. In IntelliTrace I can see that an exception is thrown "A data source instance has not been supplied for the data source 'IssRep'" (IssRep is the dataset name in the report. Here is the code:
Dim warning As Warning() = Nothing
Dim streamids As String() = Nothing
Dim mimetype As String = Nothing
Dim encoding As String = Nothing
Dim extension As String = Nothing
Dim viewer As New ReportViewer
Dim bs As New BindingSource
bs.DataSource = issuedet
Dim rds As New ReportDataSource
rds.Value = bs
viewer.LocalReport.DataSources.Add(rds)
viewer.ProcessingMode = ProcessingMode.Local
viewer.LocalReport.ReportEmbeddedResource = "FRSFE.SR.rdlc"
Dim pdfbytes As Byte()
Try
pdfbytes = viewer.LocalReport.Render("PDF", Nothing, mimetype, encoding, extension, streamids, warning)
File.WriteAllBytes("C:\Shared\FRS\SR.PDF", pdfbytes)
Catch ex As Exception
MsgBox(ex.Message)
End Try
I'm pretty sure whatever I'm stuck on is pretty simple as I'm very rusty on .NET but I just can't figure it out!
Try setting rds.Name = "IssRep" before adding it to viewer.LocalReport.DataSources.

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.