Results 1 to 8 of 8

Thread: [RESOLVED] Error using xml deserialize, parsing xml into a struct with stringwriter()

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Resolved [RESOLVED] Error using xml deserialize, parsing xml into a struct with stringwriter()

    Getting the following error from this code:

    "System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type xml_parser.Module1+Stats was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."


    Code:
    Imports System.IO
    Imports System.Xml
    
    Public Module Module1
    
        Sub Main()
            Dim host As New Stats
    
            host.Name = "test1"
            host.FQDN = "test1.lab.local"
            host.IP = "192.168.5.6"
            host.UserID = "user1"
    
            Console.WriteLine(ToXML(host))
            Console.ReadKey()
        End Sub
    
        'Convert Host data table to XML format for saving to file:
        Public Function ToXML(ByVal obj As Object) As String
            Try
                Dim x As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Object))
                Dim sw As New StringWriter()
                x.Serialize(sw, obj)
                Return sw.ToString
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
                Console.ReadKey()
            End Try
        End Function
    
        Public Function ToStruct(ByVal xString As String, ByVal type As Type) As Object
            Dim x As New Xml.Serialization.XmlSerializer(type)
            Dim sw As New IO.StringReader(xString)
            Return x.Deserialize(sw)
        End Function
    
        ' Host data table details:
        <Serializable()> Public Structure Stats
            Dim Name As String
            Dim FQDN As String
            Dim IP As String
            Dim UserID As String
        End Structure
    
    End Module

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Error using xml deserialize, parsing xml into a struct with stringwriter()

    By changing the ToXML method I got this to work.

    Code:
        'Convert Host data table to XML format for saving to file:
        Public Function ToXML(ByVal obj As Stats) As String
            Try
                Dim x As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(obj.GetType)
                Dim sw As New StringWriter()
                x.Serialize(sw, obj)
                Return sw.ToString
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
                Console.ReadKey()
                Return Nothing
            End Try
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Error using xml deserialize, parsing xml into a struct with stringwriter()

    Fixed with a few minor structural changes, here is the new code:

    Code:
    Imports System.IO
    Imports System.Xml.Serialization
    
    Public Class xmlstuff
        Public Shared Function ToXML(ByVal host As Stats)
            Dim serializer As XmlSerializer = New XmlSerializer(GetType(Stats))
    
            Dim writer As New StringWriter()
            serializer.Serialize(writer, host)
            Return writer.ToString
        End Function
    
        Public Shared Function ToData(ByVal xString As String, ByVal type As Type) As Object
            Dim x As New Xml.Serialization.XmlSerializer(type)
            Dim sw As New IO.StringReader(xString)
            Return x.Deserialize(sw)
        End Function
    
    End Class
    
    Public Class Stats
        Public Property Name As String
        Public Property FQDN As String
        Public Property IP As String
        Public Property UserID As String
    End Class
    
    Public Module Module1
    
        Sub Main()
            Dim host As Stats = New Stats()
    
            host.Name = "test1"
            host.FQDN = "test1.lab.local"
            host.IP = "192.168.5.6"
            host.UserID = "user1"
    
            Console.WriteLine(xmlstuff.ToXML(host))
            Console.ReadKey()
        End Sub
    
    End Module

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Error using xml deserialize, parsing xml into a struct with stringwriter()

    Thanks for this - yours is a simpler solution - I wasn't sure which one of my changes made it work.

    Quote Originally Posted by dbasnett View Post
    By changing the ToXML method I got this to work.

    Code:
        'Convert Host data table to XML format for saving to file:
        Public Function ToXML(ByVal obj As Stats) As String
            Try
                Dim x As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(obj.GetType)
                Dim sw As New StringWriter()
                x.Serialize(sw, obj)
                Return sw.ToString
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
                Console.ReadKey()
                Return Nothing
            End Try
        End Function

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: [RESOLVED] Error using xml deserialize, parsing xml into a struct with stringwrit

    So - for my general education, can you explain why the change from:

    Xml.Serialization.XmlSerializer(GetType(Object))

    to:

    Xml.Serialization.XmlSerializer(obj.GetType)

    is what made the difference?

    Thanks

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Error using xml deserialize, parsing xml into a struct with stringwriter()

    Here is what I ended up with.

    Code:
    Imports System.IO
    Imports System.Xml
    
    Public Module Module1
    
        Sub Main()
            Dim host As New Stats
    
            host.Name = "test1"
            host.FQDN = "test1.lab.local"
            host.IP = "192.168.5.6"
            host.UserID = "user1"
    
            Dim path As String = "path to save / load here"
    
            Dim xe As XElement = XElement.Parse(ToXML(host))
            ' xe.Save(path) ' to save
            ' xe = XElement.Load(path) 'to load
            Console.WriteLine(xe.ToString)
            Dim rthost As Stats
            rthost = DirectCast(ToStruct(xe, rthost), Stats)
            Console.ReadKey()
        End Sub
    
        'Convert Host data table to XML format for saving to file:
        Public Function ToXML(ByVal obj As Stats) As String
            Try
                Dim x As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(obj.GetType)
                Dim sw As New StringWriter()
                x.Serialize(sw, obj)
                Return sw.ToString
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
                Console.ReadKey()
                Return Nothing
            End Try
        End Function
    
        Public Function ToStruct(ByVal el As XElement, ByVal type As Stats) As Object
            Dim x As New Xml.Serialization.XmlSerializer(type.GetType)
            Dim sw As XmlReader = el.CreateReader
            Return x.Deserialize(sw)
        End Function
    
        ' Host data table details:
        <Serializable()> Public Structure Stats
            Dim Name As String
            Dim FQDN As String
            Dim IP As String
            Dim UserID As String
        End Structure
    
    End Module
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: [RESOLVED] Error using xml deserialize, parsing xml into a struct with stringwrit

    Nice. My next step is trying to use an array of Stats() to read/write a multi-host xml file.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: [RESOLVED] Error using xml deserialize, parsing xml into a struct with stringwrit

    Here is my simplistic approach using XElement, reading xml into the struct one field at a time. Is there a better way?

    Code:
    Module Module1
    
        Sub Main()
            Dim host As New List(Of Stats)
    
            Dim xelement As XElement = XElement.Load("xmldata.xml")
            Dim xmldata As IEnumerable(Of XElement) = xelement.Elements()
            ' Read the entire XML
            For Each stat In xmldata
                If (stat.Elements("Name").Any() And stat.Elements("FQDN").Any() And stat.Elements("IP").Any() And stat.Elements("UserID").Any()) Then
                    Try
                        Dim hostentry As Stats = New Stats(stat.Element("Name").Value, stat.Element("FQDN").Value, stat.Element("IP").Value, stat.Element("UserID").Value)
                        host.Add(hostentry)
                    Catch ex As Exception
                        Console.WriteLine(ex.ToString)
                        Console.ReadKey()
                    End Try
    
                End If
            Next stat
    
            For Each stat In host
                Console.WriteLine("{0} has IP of {1} and UserID of {2}", stat.Name, stat.IP, stat.UserID)
            Next
    
            Console.ReadKey()
        End Sub
    
        <Serializable()> Public Structure Stats
            Dim Name As String
            Dim FQDN As String
            Dim IP As String
            Dim UserID As String
            Public Sub New(N As String, F As String, I As String, U As String)
                Name = N
                FQDN = F
                IP = I
                UserID = U
            End Sub
        End Structure
    End Module
    Last edited by bigteks; Apr 13th, 2018 at 02:29 PM. Reason: Fixed a couple of typos in code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width