Help? Getting rid of invalid characters?http://forums.asp.net/t/329486.aspx/1?Help+Getting+rid+of+invalid+characters+Thu, 04 Sep 2003 18:11:12 -0400329486329486http://forums.asp.net/p/329486/329486.aspx/1?Help+Getting+rid+of+invalid+characters+Help? Getting rid of invalid characters? Hello all. I have an application that reads records from a database and builds an XML object from them. For certain reasons, I have to manually create the XML in code. I loop through the items and call a function I wrote called XMLFriendly(), which /supposedly/ returns an XML-safe string. The problem is that it doesn't work. There are so many potential illegal characters in XML and our users tend to cut and paste from other documents into the database. As a result, I get all sorts of ASCII characters that are invalid in XML. So, what I need is a function that is something like this: MyXMLString = XMLEncode(MyString) Something in the same vain as the old ASP <i>Server.URLEncode</i> function. Is there anything like this? Right now, my function looks like this: <pre class="prettyprint">Private Function XMLFriendly(ByVal s As String) As String s = Replace(s, &quot;&amp;&quot;, &quot;&amp;&quot;) s = Replace(s, &quot;&lt;&quot;, &quot;&lt;&quot;) s = Replace(s, &quot;&gt;&quot;, &quot;&gt;&quot;) s = Replace(s, &quot;'&quot;, &quot;&amp;apos;&quot;) s = Replace(s, &quot;&quot;&quot;&quot;, &quot;&quot;&quot;) Return (s) End Function</pre> As you can see, this isn't the wisest way to do things, because it would require you to handle all possible erroneous XML characters. I found something called XmlConvert.EncodeName(...), but it seems to work in a wierd way. It converts items to _x0000_, which doesn't help me in my HTML output. Thanks a ton, I am pulling my hair out on this one. 2003-09-04T15:35:31-04:00329532http://forums.asp.net/p/329486/329532.aspx/1?Re+Help+Getting+rid+of+invalid+characters+Re: Help? Getting rid of invalid characters? You could always wrap the text in cdata tags : 2003-09-04T16:13:00-04:00329687http://forums.asp.net/p/329486/329687.aspx/1?Re+Help+Getting+rid+of+invalid+characters+Re: Help? Getting rid of invalid characters? This actually works for standard characters like ampersand, quote, etc, but I still get an error when I insert characters like these: Horizontal tab (ASCII 0x09) &quot;\\t&quot; Linefeed or newline (ASCII 0x0A) &quot;\\n&quot; Vertical tab (ASCII 0x0B) &quot;\\v&quot; Form feed (ASCII 0x0C) &quot;\\f&quot; Carriage return (ASCII 0x0D) &quot;\\r&quot; Thanks for the suggestion, but it doesn't really solve the problem entirely. Helj 2003-09-04T17:32:40-04:00329697http://forums.asp.net/p/329486/329697.aspx/1?Re+Help+Getting+rid+of+invalid+characters+Re: Help? Getting rid of invalid characters? I don't think that I explained myself ... CDATA tells the parser to ignore the text as it may have invalid chars in it. Check this out for more info: <a href="http://www.w3schools.com/xml/xml_cdata.asp">http://www.w3schools.com/xml/xml_cdata.asp</a> 2003-09-04T17:40:37-04:00329736http://forums.asp.net/p/329486/329736.aspx/1?Re+Help+Getting+rid+of+invalid+characters+Re: Help? Getting rid of invalid characters? That would seem to make sense to me, except that when I actually try it, I get this error message during parsing: <b>' ', hexadecimal value 0x0C, is an invalid character. Line 8, position 1. </b> Here is the XML that is being parsed (the error is occurring where you see the characters): <pre class="prettyprint">This is a test &amp; an ampersand. A single ' quote, a double &quot; and a semi-colon ; ]]&gt;</pre> 2003-09-04T18:09:51-04:00