I am trying to write a class to serialize/deserialize something as simple as:
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
I have gotten to the point of (see below) but just can’t get it to generate the table xml correctly:
Imports System.Xml.Serialization
<XmlRoot("TABLE")>
Public Class HTMLTable
<XmlArray("")>
<XmlArrayItem(GetType(HTMLTableRow), elementName:="TR")>
Public Property Rows As New List(Of HTMLTableRow)
<XmlType("TR")>
Public Class HTMLTableRow
<XmlArray("TD")>
Public Property Cells As New List(Of HTMLTableCell)
End Class
<XmlType("TD")>
Public Class HTMLTableCell
<XmlText()>
Public Property Value As String
End Class
End Class
With a test like:
<Test()>
Public Sub SerializeTest()
Dim tbl As New HTMLTable
Dim row As New HTMLTable.HTMLTableRow
Dim cell As New HTMLTable.HTMLTableCell
row.Cells.Add(cell)
tbl.Rows.Add(row)
Debug.Print(XMLProcessor.Serialize(tbl))
End Sub
I just can’t get it to serialize to this thing. I tried to generate the class using xsd but that produced a lot of garbage code and I really would like to maintain this by hand as it is easier than the monstrosity that xsd produced which didn’t work anyway. What am I doing wrong above?
Try changing:
into:
, and change:
into:
That is how I would set up the attributes when using
XmlSerializerfor the same task.I would also move the
HTMLTableRowandHTMLTableCellclasses out of theHTMLTableclass.On another note, your test method isn’t really a test method, since it doesn’t do any
Assertand it doesn’t throw an exception when the result doesn’t meet your requirements!