In our SQL Server database we have in a text
field records which are strings but are written as xml with random nodes i.e.
<Record id="5">
<account>
<OldValue>125025</OldValue>
<NewValue></NewValue>
</account>
<Amount>
<OldValue>001995</OldValue>
<NewValue></NewValue>
</Amount>
</Record id>
or
<Stock>
<Car>
<OldValue>035</OldValue>
<NewValue>038 </NewValue>
</Car>
</Stock>
I need to make these records readable in a string for a report such as:
ID: 5,
Account Old Value: 125025,
Account New Value: -
Amount Old Value: 001995,
Amount New Value: -
I don't think I can serialize
the string because I don't have a predifined class to serialize it with. I'm in the process of creating a function to read my random xml nodes but I'm not sure if I'm following the right approach. What is more, at the moment I'm getting an error: XMLException: 'id' is an unexpected token.
Public Shared Function XmlCustom(sValue As String)
Dim output As StringBuilder = New StringBuilder()
If Not String.IsNullOrEmpty(sValue) Then
Dim xmlString As String = sValue
Try
' Create an XmlReader
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
Dim ws As XmlWriterSettings = New XmlWriterSettings()
ws.Indent = True
Using writer As XmlWriter = XmlWriter.Create(output, ws)
' Parse the file and display each of the nodes.
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
writer.WriteStartElement(reader.Name)
Case XmlNodeType.Text
writer.WriteString(reader.Value)
Case XmlNodeType.XmlDeclaration
Case XmlNodeType.ProcessingInstruction
writer.WriteProcessingInstruction(reader.Name, reader.Value)
Case XmlNodeType.Comment
writer.WriteComment(reader.Value)
Case XmlNodeType.Attribute
writer.WriteElementString("id", reader.Value)
Case XmlNodeType.EndElement
writer.WriteFullEndElement()
End Select
End While
End Using
End Using
Catch ex As Exception
MsgBox(output.ToString())
MessageBox.Show("XmlCustom - " & ex.ToString)
End Try
End If
End Function
Any ideas how to tackle this problem?