2012-10-02 3 views
24

나는 Windows Service을 만들고 있습니다. Service은 매일 밤 무언가를 가져와야하기 때문에 나중에 변경할 필요가 있기 때문에 App.Config에 URI를 배치하고 싶습니다.App.Config에서 URI 문자열을 쓰는 방법

App.Config에 URI를 쓰고 싶습니다. 무엇이 그것을 무효로 만들고 어떻게 접근해야합니까?

<appSettings> 
    <add key="fooUriString" 
     value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"/> 
</appSettings> 

내 오류 :

- Entity 'login' not defined 
- Expecting ';' 
- Entity 'password' not defined 
- Application Configuration file "App.config" is invalid. An error occurred 
+1

http://stackoverflow.com/questions/1328538/how-do-i-escape-ampersands-in-xml 그냥 앰퍼샌드를 벗어납니다. –

답변

50

당신은 제대로 URI에 앰퍼샌드를 인코딩하지 않았습니다. app.config은 XML 파일이므로 이스케이프 관련 XML 요구 사항을 준수해야합니다 (예 : &&amp;, <&lt;, >&gt;이어야 함). 귀하의 경우에는

, 그것은 다음과 같아야합니다

<appSettings> 
    <add 
     key="fooUriString" 
     value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&amp;login=null&amp;password=null" 
    /> 
</appSettings> 

그러나 일반적으로를, 당신은 다음이 할 "I <3 angle bra<kets & ampersands >>>"처럼 보였다 문자열을 저장하기를 원한다면 :

<appSettings> 
    <add 
     key="someString" 
     value="I &lt;3 angle bra&lt;kets &amp; ampersands &gt;&gt;&gt;" 
    /> 
</appSettings> 

void StringEncodingTest() { 
    String expected = "I <3 angle bra<kets & ampersands >>>"; 
    String actual = ConfigurationManager.AppSettings["someString"]; 
    Debug.Assert.AreEqual(expected, actual); 
} 
+0

답변을 주셔서 감사합니다. 당신이 처음 이었기 때문에, SO 신이 저를 허용 할 때 몇 명의 미성년자들에게 당신의 것을 받아 들일 것입니다. – radbyx

7

하여 사용해보십시오를 : &amp; 대신에 & url

관련 문제