-1
我使用XML文件來存儲一些鍵/值數據寫入新行字符:讀/從/到XML
<Resource Key="A1" Value="Some text" />
我有問題是什麼,是我將如何保存價值/加載數據,如果它是一個多行文本?
<Resource Key="A2" Value="Some text\nin two lines" />
應,顯示時,導致
Some text
in 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\\nin two lines.
那麼,我怎麼讀/保存一個新的lin e字符是一些文本,例如,以後可以在WPF應用程序或Web應用程序中顯示?
編輯:這裏有一個例子(正確地寫,是不正確的):
<!-- 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
}
你有沒有嘗試過用一個新行輸出一些xml來查看它在內部做了些什麼? – 2013-02-14 17:44:21
99%您正在查看調試器中的字符串,並顯示您已轉義'\'。請注意,目前還不清楚你期望在那裏看到什麼。 – 2013-02-14 17:46:13
在調試器中,在wpf窗口中,沒有任何區別。我將編輯這個問題,讓你們所有人都可以輕鬆測試。 – Goran 2013-02-14 18:03:15