Announcement

Collapse
No announcement yet.

Serialization and Public Classes

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

  • Serialization and 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.

  • #2
    Are you trying this via tenScripting or from the HS3/scripts folder?
    tenholde

    Comment


    • #3
      I placed the following into a tenScripting .vb file and it ran fine, creating the data.xml file properly:
      Code:
      Public Class Tester
          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
      End Class
      Note that I moved the first END CLASS to the end of the file.

      tenholde

      Comment


      • #4
        I don't have a solution, however it may because scripts are not pure and wrapped in code by HS3. Perhaps Rich rjh can confirm?
        Jon

        Comment


        • #5
          Sorry for posting two threads on this topic.
          Tenholde has confirmed the “public class” issue on the other thread.
          https://forums.homeseer.com/forum/de...public-classes

          I am completely lost and have no idea how to solve this unexpected problem.

          Comment


          • #6
            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