Strip out invalid characters from xml files [modified]
-
Can someone help me with the following: 1- I have xml text data that i need to take out invalid characters from. 2- this data contains extraneous characters that i need removed in order to use
xmlDocument.LoadXml(str)
3- when this characters are in thestr
variable i get an XmlException that indicates that there's an invalid character with hex value of 0x0B. 4- how can i remove that character from the string prior to loading it in withxmlDocument.LoadXml(str)
? 5- i am using VS2005 Your help is greatly appreciated....:confused: -- modified at 23:11 Thursday 17th August, 2006 -
Can someone help me with the following: 1- I have xml text data that i need to take out invalid characters from. 2- this data contains extraneous characters that i need removed in order to use
xmlDocument.LoadXml(str)
3- when this characters are in thestr
variable i get an XmlException that indicates that there's an invalid character with hex value of 0x0B. 4- how can i remove that character from the string prior to loading it in withxmlDocument.LoadXml(str)
? 5- i am using VS2005 Your help is greatly appreciated....:confused: -- modified at 23:11 Thursday 17th August, 2006The string class has a replace method, you would generally call that to remove anything you didn't want in there.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Can someone help me with the following: 1- I have xml text data that i need to take out invalid characters from. 2- this data contains extraneous characters that i need removed in order to use
xmlDocument.LoadXml(str)
3- when this characters are in thestr
variable i get an XmlException that indicates that there's an invalid character with hex value of 0x0B. 4- how can i remove that character from the string prior to loading it in withxmlDocument.LoadXml(str)
? 5- i am using VS2005 Your help is greatly appreciated....:confused: -- modified at 23:11 Thursday 17th August, 2006 -
Can someone help me with the following: 1- I have xml text data that i need to take out invalid characters from. 2- this data contains extraneous characters that i need removed in order to use
xmlDocument.LoadXml(str)
3- when this characters are in thestr
variable i get an XmlException that indicates that there's an invalid character with hex value of 0x0B. 4- how can i remove that character from the string prior to loading it in withxmlDocument.LoadXml(str)
? 5- i am using VS2005 Your help is greatly appreciated....:confused: -- modified at 23:11 Thursday 17th August, 2006Thanks to Christian and Guffa. Below is the solution I was able to come up with...
Public Structure HexReplacement
Public HexCharacter As Char
Public Replacement As String
End StructurePublic Shared Function ConvertHexToChar(ByVal hexValue As String) As Char
Dim convertedChar As Char = Nothing
Try
convertedChar = Chr(Int32.Parse(hexValue, Globalization.NumberStyles.HexNumber))
Catch ex As FormatException
' Error handling here....
Catch ex As ArgumentException
' Error handling here....
End Try
Return convertedChar
End FunctionPublic Function CleanOutHexValues(ByVal dirtyString As String, ByVal hexReplacements As HexReplacement()) As String
Dim cleanString As String = dirtyString
For Each hr As HexReplacement In hexReplacements
cleanString = cleanString.Replace(hr.HexCharacter, hr.Replacement)
Next
Return cleanString
End FunctionPublic Sub TestHexCharCleanUp()
Dim hr(1) As HexReplacement
hr(0).HexCharacter = ConvertHexToChar("0b") ' "0b" = or single space.
hr(0).Replacement = ""
Dim dirtyString As String = "Testing the removal of "
MsgBox(CleanOutHexValues(dirtyString, hr))
End SubComments will be greatly appreciated. :)