简体   繁体   中英

C# replace certain text within .plist / XML file?

I am working on an application that will need to feed data into a .plist file. The places that need to be replaced have my custom text of something like {Text-Placeholder} or {BackgroundColor-Placeholder} .

Is there a way with C# where I can essentially just replace the instances of these? Any help would be great, thanks!

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);

In many cases, plist files are just XML files, so you can simply load and save them as any other text file. The only thing you have to pay attention to is Encoding.

File.WriteAllText will by default use UTF-8 encoding. So, when the plist file starts with

<?xml version="1.0" encoding="UTF-8"?>

then everything is alright. If the plist file uses a different encoding, you would have to specify that encoding as an additional parameter to File.WriteAllText .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM