Deserialize file with "invalid" classnames

  • Thread starter Thread starter sleipner
  • Start date Start date
S

sleipner

I need some help in deserializing a file. I do manage to do that when the
xml-nodes (?) have valid classnames. However I have some problems when the
xml-node has a name which is not valid as a classname, like the example below.

It's not allowed to name the class M-Peaches.

<M-Peaches>
<M-Peach name="1">
<\M-Peach>
<M-Peach name="2">
<\M-Peach>
<\M-Peaches>
 
sleipner said:
I need some help in deserializing a file. I do manage to do that when the
xml-nodes (?) have valid classnames. However I have some problems when the
xml-node has a name which is not valid as a classname, like the example below.

It's not allowed to name the class M-Peaches.

<M-Peaches>
<M-Peach name="1">
<\M-Peach>
<M-Peach name="2">
<\M-Peach>
<\M-Peaches>

The class that serialized this should be able to deserialize it. If you are
working outside this code and trying to deserialize without the original
code, then you probably need to do something like this:

Public Class Peach
<XmlAttribute("name")> _
Public Name As String = "Georgia"
Public Sub New()
End Sub
Public Sub New(ByVal n As String)
Name = n
End Sub
End Class
Public Class PeachBasket
<XmlElement("M-Peach")> _
Public Peaches As New List(Of Peach)
Public Sub New()
Peaches.Add(New Peach("Georgia"))
Peaches.Add(New Peach("Alabama"))
End Sub
End Class
Public Class Truck
<XmlElement("M-Peaches")> _
Public pb As New PeachBasket
End Class
 
This one was already very helpful, thanks a lot.

But I'd like to ask one more question.

If you have a look at the code snippet below, I've done it a bit more direct
until now.
I've defined a class "Apple", and to read in the list of Apples this line
seems to work fine.

Public Apples() As Apple

A bit more complicated with the <M-Peaches>, but as far as I can see this
should partly work. It solves the problem with the <M-Peaches>, but it
doesn't solve the problem with the <M-Peach>. Could the line below be
modified somehow to fix the problem?

<XmlElement("M-Peaches")> Public Peaches() As Peach
 
The file i am trying to deserialize is mostly hand-edited. I want to try to
use the deserialize method, as I find it most promising.
 
sleipner said:
This one was already very helpful, thanks a lot.

But I'd like to ask one more question.

If you have a look at the code snippet below, I've done it a bit more
direct
until now.
I've defined a class "Apple", and to read in the list of Apples this line
seems to work fine.

Public Apples() As Apple

A bit more complicated with the <M-Peaches>, but as far as I can see this
should partly work. It solves the problem with the <M-Peaches>, but it
doesn't solve the problem with the <M-Peach>. Could the line below be
modified somehow to fix the problem?

<XmlElement("M-Peaches")> Public Peaches() As Peach


You should probably look at XSD.exe, which is distributed with the full
visual studio. I tried to find it in express, and cannot. It can generate
an xsd file from xml, and from the xsd, generate either a vb or csharp class
definition. This may be a good start for you.
 
sleipner said:
The file i am trying to deserialize is mostly hand-edited. I want to
try to use the deserialize method, as I find it most promising.

Maybe you could first parse it as a text file to remove errors (could you
substitute M-Peaches with something valid?) or alert the user?

Andrew
 
Back
Top