2013-02-14 1 views
-1

에 /에서 새 줄 문자를 작성 :읽기/I 몇 가지 키/값 데이터를 저장하기 위해 XML 파일을 사용하여 XML

<Resource Key="A1" Value="Some text" /> 

내가 문제가 누구인지, 어떻게 값에/부하 데이터를 저장하는 것이됩니다, 그것이 여러 줄짜리 텍스트라면?

<Resource Key="A2" Value="Some text\nin two lines" /> 

표시 될 때 나는

XDocument document = XDocument.Load(filePath); 

// get all the localized client resource strings 
var resource = (from r in document.Descendants("Resource") 
          where r.Attribute("Key").Value == "A2" 
          select r).SingleOrDefault(); 

를 사용하여 자원 위에 읽는다면,

Some text 
in two lines 

발생해야하는이 이중 백 슬래시를 읽습니다 :

Some text\\nin two lines. 

그래서, 어떻게 새로운 lin을 읽고 저장할 수 있습니까? e 문자는 나중에 WPF 응용 프로그램이나 웹 응용 프로그램에 표시 될 수있는 텍스트입니다.

편집는 예를 들면 다음과 같습니다 (정확하게 기록, 잘못 읽기) :

<!-- WPF window xaml code --> 
<Grid> 
    <Button Name="btn" Content="Click me" /> 
</Grid> 

// WPF window code behind 
public MainWindow() 
{ 
    InitializeComponent(); 

    XDocument doc = 
      new XDocument(
      new XElement("Resources", 
       new XElement("Resource", new XAttribute("Key", "A1"), new XAttribute("Value", @"Some text\nin two lines"))) 
     ); 

    const string fileName = @"D:\test.xml"; 
    doc.Save(fileName); 

    doc = XDocument.Load(fileName); 

    IDictionary<string, string> keys = (from c in doc.Descendants("Resource") 
             select c).ToDictionary(c => c.Attribute("Key").Value, c => c.Attribute("Value").Value); 

    btn.ToolTip = keys["A1"]; 
    //btn.ToolTip = "Some text\nin two lines"; // if you uncomment this line, it works as expected 
} 
+0

내부적으로 수행 할 작업을 확인하기 위해 새 줄을 사용하여 일부 XML 출력을 시도 했습니까? –

+1

99 % 당신은 디버거에서 문자열을보고 있으며 그것은 당신이 이스케이프 된'\'을 보여줍니다. 그곳에서 당신이보기를 기대하는 것은 분명하지 않습니다. –

+0

디버거의 wpf 창에서 아무런 차이가 없습니다. 여러분 모두가 쉽게 테스트 할 수 있도록 질문을 편집 할 것입니다. – Goran

답변

0

사람이 (XML에서 읽는 동안 탈출 방지하는 방법을 알고 있다면 문자열이

btn.ToolTip = System.Text.RegularExpressions.Regex.Unescape(keys["A1"]); 

작동 언 이스케이프 그것이 그대로 이스케이프되지 않은 상태로 읽는 것), 그것에 대해 듣게되어 기쁩니다.

관련 문제