Question Problems while passing strings with single quotation marks to as xml

rva1945

Member
Joined
Jun 28, 2010
Messages
12
Programming Experience
Beginner
I use the ReadXml function to read the xml schema and sore it into a dataset. But one of the string type fields can have expressions like this:

gender='F' and state='A'

and those single quotation marks cause the xml string to break with obvious consecuences. What should I do to conveniently transform it?

Thanks
 
This is the line that raises the error:
DataSetD.ReadXml(New IO.StringReader(oXml))

oXML is a dataset in XML format.

table(5).rows(0).columns(3) is string and equals to something like this: (gender='F') and (status='A'), and in this case, instead if being formatted the right way, i.e.:

.....<field> (gender='F') and (status='A') </field> </tablename> </dataset>"

it breaks at the first single quotation mark, so

.....<field> (gender= "

and it crashes. The dataset (DSD) is passed as XML this way: DSD.GetXml()

Of course everything works fine if no single quotation mark is present. And of course it crashes no matter which field includes that character.

I'd appreciate your help.
 
I feel like there should be something automatic to handle this but I really don't know if there is. Failing anything else being available, you could try escaping all the single quotes with another single quote, which is often the way special characters are treated. You could do this:
VB.NET:
oXml = oXml.Replace("'", "''")
as long as you know that there are no single quotes that shouldn't be escaped.

By the way, why is 'oXml' named 'oXml'? If that is Hungarian Notation then it's pointless and wrong. The point of Hungarian Notation is to indicate the type of a variable. "o" is often used as a default but it's basically pointless because everything is an object so it tells you nothing. In this case though, 'oXml' is presumably type String, so the prefix should be "s", "sz", "str" or something useful. I hate Hungarian Notation and don't think it should be used at all but if you're going to use it then use it consistently, otherwise it's misleading, which is the exact opposite of why it's supposedly used in the first place, so it's worse than pointless.
 
I don't get it, any character that needs escaping is escaped automatically for xml output (using ds.GetXml or ds.WriteXml) and unescaped for xml input (ds.ReadXml), and even so the single-quote char is not one of them. Does either one of you have additional information/code that reproduce this error, because the information provided so far does not produce any error with this code:
VB.NET:
Dim tbl As New DataTable
tbl.Columns.Add("col1", GetType(String))        
tbl.Rows.Add("(gender='F') and (status='A')")
Dim ds As New DataSet
ds.Tables.Add(tbl)
Dim xml As String = ds.GetXml
ds.Clear()
ds.ReadXml(New IO.StringReader(xml))
Debug.WriteLine(ds.Tables(0)(0)(0))
To see an example of automatic character escaping, add the & char to the string value and inspect the xml output.
 
Back
Top