-1

xmlファイルを使用していくつかのキー/値データを保存します:

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

私が問題を抱えているのは、データが複数行のテキストである場合、どのようにデータをValueに保存/ロードするのですか?

<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.

では、たとえば、後で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
}
4

1 に答える 1

0

文字列のエスケープを解除すると機能します

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

誰かがxmlからの読み取り中にエスケープを回避する方法を知っている場合(そのままの状態で読み取るため)、私はそれについて聞いてうれしいです。

于 2013-02-14T18:52:20.340 に答える