I have to get the data from an XML file to an object. I have already do this for a simple class, but for one with nested children as array of elements I've not receiving the values from enumeration, only from main part.
Is there something special to do in order to get the nested items?
I have an xsd file from a third part company with the structure of the xml file. They are also sending me the xml files on that structure.
I've created my vb.net class using xsd.exe from Visual Studio. The class is big enough to be hard done manually.
I don't know how to get all data from my xml using deserializing method. If I use an dataset, I get all data, but it is very hard to convert the dataset to my object.
How can I deserialize all nested objects?
Here are the elements. You can download the files at GoogleDrive.
The XSD files: PathDetailsMessage.xsd, Common_Elements.xsd
The class generated: PathDetailsMessage.vb
The XML file: OneTrain.XML
My code:
Dim obj As PathDetailsMessage
Dim ser As New XmlSerializer(GetType(PathDetailsMessage))
Using reader As XmlReader = XmlReader.Create(fullPath)
obj = CType(ser.Deserialize(reader), PathDetailsMessage)
End Using
The properties with abnormal null (nothing) values.
obj.PathInformation
obj.CFRTrainInfo
I have to use something like this:
For Each pjl As PlannedJourneyLocation In obj.PathInformation.PlannedJourneyLocation
''
Next
but obj.PathInformation
is nothing!
OK I think the issue is that you're running an extra xsd.exe
command that is unnecessary to generate your code. The only command required is:
xsd .\PathDetailMessage.xsd /l:VB /c /namespace:infofer
Here's an example of my working code where obj.PathInformation
and obj.CFRTrainInfo
are not null, and properly populated (run the ConsoleTest project to see for yourself):
https://github.com/mamift/SO_76532514/tree/master/src/lib/OneTrain
I can see in your provided code you've run xsd.exe
twice on both schemas, and then manually edited the generated code in PathDetailsMessage.vb to remove it from the infofer
namespace, which resolved build errors you would've gotten regarding duplicate declarations. You'll also notice you've got classes like AdministrativeContactInformation
declared twice, once in PathDetailsMessage.vb and agin in Common_Elements.vb.
If you take a look at PathDetailsMessage.xsd, there's this include directive on line 3:
<xs:include schemaLocation="Common_Elements.xsd"/>
Because Common_Elements.xsd is included by the PathDetailsMessage.xsd file, xsd.exe is smart enough to import those elements and generate code for that file alongside PathDetailMessage.xsd. It's just that the tool does not split the generated code into a separate Common_Elements.vb file.