Getting DataTable to clean XML to send over a socket
-
Hi Guys, In a Client/Server application i was writing, i used XML in my own little protocol to send data between the client and the server in a form something like this:
CLIENTID|PROTOCOL_COMMAND_INTEGER|I will be you data for today|
Now in the past i've strugled with this xml part because i need encoding that can handle é,è,ç,à and other funny characters like & and stuff.. for this, i could not use datatable.WriteXml(memorystream) (i think) because it always messed up these characters. Being the starting developer that I am , i created a little piece of horrorcode looking like this:Public Function GetXmlFromRow(ByVal dr As DataRow, ByVal sRecordType As String) As String
Dim sb As New IO.StringWriter
Dim xwXmlTextWriter As New XmlTextWriter(sb)
xwXmlTextWriter.WriteStartDocument()
xwXmlTextWriter.WriteStartElement(sRecordType)
Dim col As DataColumn
For Each col In dr.Table.Columns
xwXmlTextWriter.WriteElementString(col.ColumnName, dr.Item(col).ToString)
Next
xwXmlTextWriter.WriteEndDocument()
xwXmlTextWriter.Flush()
Return sb.ToString
End FunctionIt isn't pretty.. but it does the job. Now however, i ran into trouble: i need to send a blob in this XML (a blob which contains another XML file :s). however, i cannot do a ".ToString" from a blob cause it just returns the datatype name as a string. How can i get a decent formatted XML file from a datatable to send over a socket OR Do i need to rethink my solution completly ? If so: What is the best way/protocol to handle these kinds of things ? Thanks !
-
Hi Guys, In a Client/Server application i was writing, i used XML in my own little protocol to send data between the client and the server in a form something like this:
CLIENTID|PROTOCOL_COMMAND_INTEGER|I will be you data for today|
Now in the past i've strugled with this xml part because i need encoding that can handle é,è,ç,à and other funny characters like & and stuff.. for this, i could not use datatable.WriteXml(memorystream) (i think) because it always messed up these characters. Being the starting developer that I am , i created a little piece of horrorcode looking like this:Public Function GetXmlFromRow(ByVal dr As DataRow, ByVal sRecordType As String) As String
Dim sb As New IO.StringWriter
Dim xwXmlTextWriter As New XmlTextWriter(sb)
xwXmlTextWriter.WriteStartDocument()
xwXmlTextWriter.WriteStartElement(sRecordType)
Dim col As DataColumn
For Each col In dr.Table.Columns
xwXmlTextWriter.WriteElementString(col.ColumnName, dr.Item(col).ToString)
Next
xwXmlTextWriter.WriteEndDocument()
xwXmlTextWriter.Flush()
Return sb.ToString
End FunctionIt isn't pretty.. but it does the job. Now however, i ran into trouble: i need to send a blob in this XML (a blob which contains another XML file :s). however, i cannot do a ".ToString" from a blob cause it just returns the datatype name as a string. How can i get a decent formatted XML file from a datatable to send over a socket OR Do i need to rethink my solution completly ? If so: What is the best way/protocol to handle these kinds of things ? Thanks !
Hmmm seems you have 2 distinct problems. 1. Unicode in a client dataset - I'm sure it can be done, but I have not had the requirement.
Noctris wrote:
How can i get a decent formatted XML file from a datatable
2. Datatabe/set to xlm is simply xmlDataset.writexml(memorystream/variable) I think!
Never underestimate the power of human stupidity RAH
-
Hi Guys, In a Client/Server application i was writing, i used XML in my own little protocol to send data between the client and the server in a form something like this:
CLIENTID|PROTOCOL_COMMAND_INTEGER|I will be you data for today|
Now in the past i've strugled with this xml part because i need encoding that can handle é,è,ç,à and other funny characters like & and stuff.. for this, i could not use datatable.WriteXml(memorystream) (i think) because it always messed up these characters. Being the starting developer that I am , i created a little piece of horrorcode looking like this:Public Function GetXmlFromRow(ByVal dr As DataRow, ByVal sRecordType As String) As String
Dim sb As New IO.StringWriter
Dim xwXmlTextWriter As New XmlTextWriter(sb)
xwXmlTextWriter.WriteStartDocument()
xwXmlTextWriter.WriteStartElement(sRecordType)
Dim col As DataColumn
For Each col In dr.Table.Columns
xwXmlTextWriter.WriteElementString(col.ColumnName, dr.Item(col).ToString)
Next
xwXmlTextWriter.WriteEndDocument()
xwXmlTextWriter.Flush()
Return sb.ToString
End FunctionIt isn't pretty.. but it does the job. Now however, i ran into trouble: i need to send a blob in this XML (a blob which contains another XML file :s). however, i cannot do a ".ToString" from a blob cause it just returns the datatype name as a string. How can i get a decent formatted XML file from a datatable to send over a socket OR Do i need to rethink my solution completly ? If so: What is the best way/protocol to handle these kinds of things ? Thanks !
You will need to run a REGEX session on your string before you send it to your XMLWriter. Search GOOGLE for xml unsafe characters and ASCII code to see what you should turn those funny characters into. Since you are making your own server / client application you can set your own REGEX standards for conversion of characters. IE: REGEX finds a "&" in your input string. You replace "&" with a @045; and send that to your XMLWriter.