I'm trying to take a Class, define an Object with that Class, then write that Object to XML. It seemed like I could just make a Class (posted below) with 'Public Shared's, create a Public Shared object defined by that class (Character), populate it with data, then write it out to XML directly. I've hit a bunch of walls, from reference errors to the created object "writing", but being blank, despite having values in it.
I can only assume I'm lacking a fundamental understanding of how to write XML from vb.net, and despite a ton of research, I don't feel any closer to a solution here.
Class:
Public Class Character
Public Class CharacterStats
<XmlElement("CoreList")>
Public Shared ListCore As CoreStats
End Class
Public Class PowerList
<XmlElement("PowerList")>
Public Shared ListPowers As List(Of Power)
End Class
Public Class EdgeList
<XmlElement("EdgesList")>
Public Shared ListEdges As Edges
End Class
Public Class WeaponList
<XmlElement("WeaponList")>
Public Shared ListWeps As List(Of Weapon)
End Class
Public Class ArmorList
<XmlElement("ArmorList")>
Public Shared ListArm As List(Of Armor)
End Class
Public Class VehicleList
<XmlElement("VehicleList")>
Public Shared ListVeh As List(Of Vehicle)
End Class
End Class
Defined object(?):
Public Class GVar
Public Shared Property CharacterFile As New Character
End Class
Then, tried to write it out to XML in a sub:
Dim serializer As New XmlSerializer(GetType(Character))
Dim writer As New StreamWriter("C:\temp\newCharacter.STC")
serializer.Serialize(writer, GVar.CharacterFile)
writer.Close()
While this "works", the XML file it outputs is blank, other than a header:
<?xml version="1.0" encoding="utf-8"?>
I'm sure I'm just missing something brutally obvious here, but it's completely escaping me.
EDIT/update:
Each of the Public Shared items are defined from other Classes, each with their own set of values. I'll pull one of them as an example, though they're all structured similarly:
Public Class Power
'Array that stores Powers for active character
Public Name As String
Public PRating As Integer
Public Action As String
Public Cost As Integer
Public PoolNone As String
Public Pool1 As String
Public Pool2 As String
Public Range As String
Public Duration As String
Public Tags As New List(Of String)
Public Desc As String
End Class
So in this case, the Public Class "Character" has (as it's second entry) Public Shared ListPowers, as a List of "Power", as defined here. Initially this was done to initialize an object for each of the various class types (Core, Power, etc), which were read/written from internally.
It was my hope that I could create a root to nest all of those sub-classes into one XML file, without having to break them all up into their own XML files on saving.
Continuation of Trying to write a List object to XML (VB.net)
CodePudding user response:
You have a bunch of unnecessary class definitions inside your class, and in those classes are Shared properties, which is probably not what you want. Here is what the Character class should look like
<XmlRoot>
Public Class Character
<XmlElement()>
Public Property Core As CoreStats
<XmlElement("Power")>
Public Property Powers As List(Of Power)
<XmlElement()>
Public Property Edges As Edges
<XmlElement("Weapon")>
Public Property Weapons As List(Of Weapon)
<XmlElement("Armor")>
Public Property Armors As List(Of Armor)
<XmlElement("Vehicle")>
Public Property Vehicles As List(Of Vehicle)
End Class
Public Class CoreStats
' properties in here in your implementation
End Class
Public Class Power
End Class
Public Class Edges
End Class
Public Class Weapon
End Class
Public Class Armor
End Class
Public Class Vehicle
End Class
Btw the attribute with "Power"
<XmlElement("Power")>
Public Property Powers As List(Of Power)
means the Xml will have elements named Power, which are serialized into that Powers property. Without the name in the attribute the serializer will write / look for elements named as the property name (Powers) i.e.
with <XmlElement()> Public Property Powers As List(Of Power)
<Character>
<Powers>
</Powers>
<Powers>
</Powers>
</Character>
vs. <XmlElement("Power")> Public Property Powers As List(Of Power)
<Character>
<Power>
</Power>
<Power>
</Power>
</Character>
You can of course have more levels of properties for example, if the Power class has a list inside it, it may look like this
Public Class Power
<XmlElement("SubPower")>
Public Property SubPowers As List(Of SubPower)
End Class
Public Class SubPower
End Class
And the serializer will take all those additional levels of classes / properties into account i.e.
<Character>
<Power>
<SubPower>
</SubPower>
<SubPower>
</SubPower>
</Power>
<Power>
</Power>
</Character>
Btw again, you could also make the primitive properties Attributes, like this
Public Class Power
<XmlAttribute>
Public Name As String
<XmlAttribute>
Public PRating As Integer
<XmlAttribute>
Public Action As String
...
<XmlElement("Tag")>
Public Tags As New List(Of String)
End Class
<Character>
<Power Name="ecit" PRating="25" Action="Question">
<Tag>
Tag One
</Tag>
<Tag>
Tag Two
</Tag>
</Power>
</Character>