开发者

C#: Caused error :having a string variable which its value contains &?

EDIT: I have edited my post...

Working on a project (c#), I have a string (password) within an XML file (app.config) which it value contains '&' character. Suppose it some thing like this:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
     <appSettings>
        <add key="MainConnectionString" value="Data So开发者_如何学JAVAurce=MyDataSource;InitialCatalog=MyInitialCatalog;User ID=MyUserID;Password=ynhub&59=k31!890" />
    </appSettings>
  </configuration>

But compiler shows me an error and a squiggly blue line appears under 59 indicates that 'character 5 hexadecimal value is illegal in an XML name'

How can I remove this error?


Use "&amp;".


XML starts special characters with an & character, i.e. the famous &amp;, &lt; (<) and &gt; (>).

For this reason you cannot use & in your XML without converting it to &amp;. For example even in HTML links it's not allowed (but still common) to write:

<a href="foo.php?foo=bar&bar=baz">

It has to be written as:

<a href="foo.php?foo=bar&amp;bar=baz">

Noone is doing it but HTML & XML specify it that way and especially XML is very strict about it.


The & you're thinking of isn't part of the string - it's part of your source code. Carefully read the code, paying careful attention to the position of any quote marks...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜