-
Apr 13th, 2018, 09:11 AM
#1
Thread Starter
Member
[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
-
Apr 13th, 2018, 10:33 AM
#2
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
-
Apr 13th, 2018, 10:35 AM
#3
Thread Starter
Member
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
-
Apr 13th, 2018, 10:37 AM
#4
Thread Starter
Member
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.
Originally Posted by dbasnett
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
-
Apr 13th, 2018, 10:46 AM
#5
Thread Starter
Member
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
-
Apr 13th, 2018, 10:46 AM
#6
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
-
Apr 13th, 2018, 11:29 AM
#7
Thread Starter
Member
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.
-
Apr 13th, 2018, 02:17 PM
#8
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|