Announcement

Collapse
No announcement yet.

Help! XML serialization. Problem with Public classes?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help! XML serialization. Problem with Public classes?

    Hi,

    I am trying to serialize an object to xml. It is killing med!

    Imports System.IO
    Imports System.Text
    Imports System.Xml.Serialization

    Public Class Tester
    Public Shared Sub Main(ByVal Parms As Object)

    Dim theSerializer As New XmlSerializer(GetType(employee))
    Dim SGamgee As New employee("A", "G", "C")
    Dim sw As New StreamWriter("data.xml")
    theSerializer.Serialize(sw, SGamgee)
    End Sub

    End Class

    Public Class employee
    Public first_name As String
    Public last_name As String
    Public title As String

    Public Sub New()
    End Sub

    Public Sub New(ByVal first_name As String, ByVal last_name As String, ByVal title As String)
    Me.first_name = first_name
    Me.last_name = last_name
    Me.title = title
    End Sub

    End Class

    Error: "VB.Net script exception(0), re-starting: Object reference not set to an instance of an object."


    Other Errors that I have had:
    - serializing non-public classes although they are declared public. Strange?!
    - I have tried many examples that work in VS IDE, but not Homeseer....

  • #2
    I changed the code to:

    Code:
    Imports System.Text
    Imports System.IO
        Public Sub Main(ByVal Parms As Object)
    
            Dim theSerializer As New XmlSerializer(GetType(employee))
            Dim SGamgee As New employee("A", "G", "C")
            Dim sw As New StreamWriter("data.xml")
            theSerializer.Serialize(sw, SGamgee)
        End Sub
        Public Class employee
            Public first_name As String
            Public last_name As String
            Public title As String
            Public Sub New()
            End Sub
            Public Sub New(ByVal first_name As String, ByVal last_name As String, ByVal title As String)
                Me.first_name = first_name
                Me.last_name = last_name
                Me.title = title
            End Sub
        End Class
    I was confused by the first END CLASS statement.

    However, I do get:

    Code:
     
    pr-12 8:04:17 PM Error 1 Running script C:\Program Files (x86)\HomeSeer HS3\scripts\Tester.vb :Exception has been thrown by the target of an invocation.scriptcode16.VBWrapper is inaccessible due to its protection level. Only public types can be processed.
    As you said, works fine in tenScripting. Let us know if you find a solution.
    tenholde

    Comment


    • #3
      Thank you so much for responding, I really appreciate it and sorry for starting two threads. I am really desperate and find the two subforums very similar.

      The error you got is the same as I get. The serialization method requires public objects according to what I have googled. That’s where it becomes strange as the object is clearly declared as public and where the Homeseer environment comes in...

      I am lost and do not know what to do if even the “tenscripting guru” has problems. 🤔

      Who would know/understand?

      Cheers,
      Bob

      EDIT: the simplest example provided by Microsoft fails in the same way:
      https://support.microsoft.com/sv-se/...-net-or-visual

      HomeSeer must be doing something or requiring something extra.
      How to find out? I have tried all sorts of class/declaration combinations. 🙁

      Comment


      • #4
        Using Classes and the elegant ”XmlSerializer” as described above for Visual Studio, does finally not seem possible in the HomeSeer environment.

        The following function offers a workaround where “cust” is a defined object with specific properties and “XMLsettings” is defined as shared prior to the call.

        Code:
        Imports Xml.Serialization
        Imports System.Reflection
        
        Function NuvoSerialize(ByVal cust As Object) As String
            Dim sb As New StringBuilder()
            Dim sWriter As New StringWriter(sb)
            Dim xWriter As XmlWriter = XmlWriter.Create(sWriter, XMLsettings)
        
            xWriter.WriteStartDocument()
            xWriter.WriteStartElement(cust.GetType().Name.ToString)
            xWriter.WriteAttributeString("xmlns", "xsi", Nothing, "http://www.w3.org/2001/XMLSchema-instance")
            xWriter.WriteAttributeString("xmlns", "xsd", Nothing, "http://www.w3.org/2001/XMLSchema")
            For Each pi As PropertyInfo In cust.GetType.GetProperties()
                xWriter.WriteElementString(pi.Name, pi.GetValue(cust, Nothing).ToString)
            Next
            xWriter.WriteEndElement()
            xWriter.WriteEndDocument()
            xWriter.Flush()
            xWriter.Close()
        
            Return sb.ToString.Replace(" />", " xsi:nil=""true"" />")
        End Function
        Thx for the initial help in confirming the problem.

        Comment

        Working...
        X