C# 替换 .plist / XML 文件中的某些文本?
C# replace certain text within .plist / XML file?
我正在开发一个需要将数据输入 .plist
文件的应用程序。需要替换的地方有我的自定义文本,例如 {Text-Placeholder}
或 {BackgroundColor-Placeholder}
.
有没有一种方法可以使用 C# 基本上只替换它们的实例?任何帮助都会很棒,谢谢!
var fileName = @"D:\X.plist";
// Load text from file
var text = File.ReadAllText(fileName);
// Replace string
text = text.Replace("{Text-Placeholder}", "Some Text");
text = text.Replace("{BackgroundColor-Placeholder}", "Some Other Text");
// Save text to file
File.WriteAllText(fileName, text);
在许多情况下,plist 文件只是 XML 文件,因此您可以像其他任何文本文件一样简单地加载和保存它们。您唯一需要注意的是编码。
File.WriteAllText
默认使用 UTF-8 编码。所以,当 plist 文件以
开头时
<?xml version="1.0" encoding="UTF-8"?>
那么一切都好。如果 plist 文件使用不同的编码,则必须将该编码指定为 File.WriteAllText
.
的附加参数
我正在开发一个需要将数据输入 .plist
文件的应用程序。需要替换的地方有我的自定义文本,例如 {Text-Placeholder}
或 {BackgroundColor-Placeholder}
.
有没有一种方法可以使用 C# 基本上只替换它们的实例?任何帮助都会很棒,谢谢!
var fileName = @"D:\X.plist";
// Load text from file
var text = File.ReadAllText(fileName);
// Replace string
text = text.Replace("{Text-Placeholder}", "Some Text");
text = text.Replace("{BackgroundColor-Placeholder}", "Some Other Text");
// Save text to file
File.WriteAllText(fileName, text);
在许多情况下,plist 文件只是 XML 文件,因此您可以像其他任何文本文件一样简单地加载和保存它们。您唯一需要注意的是编码。
File.WriteAllText
默认使用 UTF-8 编码。所以,当 plist 文件以
<?xml version="1.0" encoding="UTF-8"?>
那么一切都好。如果 plist 文件使用不同的编码,则必须将该编码指定为 File.WriteAllText
.