• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

Deserialize in memory XML String to a structure

Hi experts,

I think I need a little help.  I am trying to deserialize an XML string received from an control event.
so that it is useable (and already parsed) in a structure inside said event.   Code is below.  All the
code works and does not err out but it is not putting the values of the xml string inside x_MySett
as I would have expected.  I've tried using both the xmlreader and textreader objects but to no
avail.  Pointers/corrections?

TIA

'** Defined at the top of the form
'** XML being received is:
'** <?xml version="1.0" encoding="us-ascii" standalone="yes"?><EVT><Time>17/10/2011 10:47:04 AM</Time></EVT>

    <Serializable()> _
    <XmlRoot("EVT")> _
    Public Structure EVTRoot
        <XmlElement("EVT")> Public EVTVal As EVTTime
    End Structure

    Public Structure EVTTime
        <XmlAttribute("Time")> Public Time As String
    End Structure

'** In my control event.
actionBuffer = Mid(validBuffer, myStart + 1, myStop - 2)

Dim bytBuffForMS() As Byte=Encoding.ASCII.GetBytes(actionBuffer)
Dim serializer As New XmlSerializer(GetType(EVTRoot))
Dim Strm As IO.MemoryStream = New IO.MemoryStream(bytBuffForMS)

Dim txtRdr As TextReader = New StreamReader(Strm)
'Dim xmlRdr As Xml.XmlReader = XmlReader.Create(Strm)

Dim x_MySett As New EVTRoot

x_MySett = serializer.Deserialize(txtRdr)

Open in new window

0
sharizod
Asked:
sharizod
  • 5
  • 2
1 Solution
 
käµfm³d 👽Commented:
Do you have a reason why EVTVal cannot be declared as string rather than EVTTime, because the following works for me:
Imports System.Xml.Serialization
Imports System.Text

Module Module1

    Sub Main()
        Dim actionBuffer As String = "<?xml version=""1.0"" encoding=""us-ascii"" standalone=""yes""?><EVT><Time>17/10/2011 10:47:04 AM</Time></EVT>"
        Dim serializer As New XmlSerializer(GetType(EVTRoot))
        Dim reader As New System.IO.StringReader(actionBuffer)
        Dim x_MySett As EVTRoot

        x_MySett = serializer.Deserialize(reader)
    End Sub

End Module

<XmlRoot("EVT")> _
Public Structure EVTRoot
    <XmlElement("Time")> Public EVTVal As String
End Structure

Open in new window

0
 
sharizodAuthor Commented:
Hi Kaufmed,

It does not work because there is Nothing in the value when I look at the contents of x_MySett.
Was there a line or two missing?  Also, when I try to move this to a module it informs me that
"<appname>.Module1 is inaccessible due to its protection level.  Only public types can be processed."

0
 
sharizodAuthor Commented:
Sorry, and no, I don't have a reason to use another datatype over a string.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
käµfm³d 👽Commented:
Was there a line or two missing?
Yes. I removed your structure definition of EVTTime and changed the type of EVTRoot.EVTVal from EVTTime to string.

"<appname>.Module1 is inaccessible due to its protection level.  Only public types can be processed."
That's my fault for not clarifying. My test was run via a Console application. You would modify your code with that found inside of the module. Namely, lines 7 - 12. You would also need to modify your structure definition to match what I have. My change occurs in line 19, and it is related to the type of the property.

This is dependent, though, on whether or not you are inclined to change your structure definitions at all. If you cannot or would not like to change the definitions, they my previous suggestion is moot.
0
 
sharizodAuthor Commented:
Hi Kaufmed,
I took your direct example and pasted it in while remming my stuff out (the event gets fired every few seconds regardless).
I was getting Nothing as the value when I would try to print out the contents via a debug.writeline statement: debug.writeline(x_MySett.EVTVal)
0
 
sharizodAuthor Commented:
Hi Kaufmed,
Sorry, it does work!  I'm not sure why it didn't before, but I'll chalk it up to PEBMAC :D
0
 
sharizodAuthor Commented:
Thanks again!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now