Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Define the allowed characters and block everything else, i.e.:</p> <pre><code>// only lowercase letters and digits if(Regex.IsMatch(yourString, @"^[a-z0-9]*$")) { // allowed } </code></pre> <p>But I think your problem may lie somewhere else, because you say it comes from serializing (valid) string and then deserializing (invalid) strings. It is possibly that you use default serialization and that you don't apply proper <code>ISerializable</code> implementation for your classes (or proper use of the <code>Serializable</code> attributes), resulting in properties or fields being serialized that you don't want to be serialized.</p> <p>PS: others have mentioned encoding issues, which is a possible cause and might mean you cannot read back the data at all. About encoding there's one simple rule: <strong><em>use the same encoding everywhere (streams, database, xml) and be specific</em></strong>. If you are not, the default encoding is used, which can be different from system to system.</p> <hr> <h3>Edit: possible solution</h3> <p>Based on new information (see thread under original question), it is pretty clear that the issue has to do with encoding. The OP mentions that it appears with dashes, which are often replaced with pretty dashes like "—" (<code>&amp;mdash;</code>) when used in some fancy editing environment. Since it seems that there's some unclarity in how to fix SQL Server to accept proper encoded strings, you can also solve this in your XML. </p> <p>When you create your XML, simply change the encoding to the most basic possible (<code>US-ASCII</code>). This will automatically force the XML writer to use the proper numerical entities. When you deserialize, this will be properly parsed in your strings without further ado. Something along these lines:</p> <pre><code>Stream stream = new MemoryStream(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.ASCII; XmlWriter writer = XmlWriter.Create(stream, settings); // make sure to output the xml-prolog header </code></pre> <p>But be aware of using <code>StringBuilder</code> or <code>StringWriter</code>, because it is fixed to using UTF-16, and the XmlWriter will always write in that encoding, <a href="http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/" rel="nofollow noreferrer">more info on that issue at my blog</a>, which is not compatible with SQL Server.</p> <p><em>Note: when using the ASCII encoding, any character higher than <code>0x7F</code> will be encoded. So, é will look like <code>&amp;#xE9</code> and the dash may look like <code>&amp;#x2014</code>, but this means just the same and you should not worry about that. Every XML capable tool will properly interpret this input.</em></p> <p><em>Note 2: the location where you want to change the way XML is written is the Web Service you talk of, that receives XML and then stores it into the SQL Server database. Before storing into SQL Server, the change must be applied. Earlier on in the chain is useless.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload