简体   繁体   English

读取文本文件并进行更改c#

[英]read Text File and Make Changes c#

i want to read a text file that Contains 我想阅读一个包含以下内容的文本文件

<CustomerName>@CoustomerName</CoustomerName>
<CustomerAddress>@CustomerAddress</CustomerAddress>
<CustomerMobileNo>@CustomerMobileNo</CustomerMobileNo>
<Payment>@Payment</Payment>

Replace this @CoustomerName with Coustomer Name Passes During Run Time 在运行时将此@CoustomerName替换为通过客户名称传递

Till then i use this 直到我用这个

string readfile = File.ReadAllText(path);
Regex.Replace(readfile , "@CoustomerName ", objProposar.FirstName);

This works But i need to make changes in Coustomer address, mobile no etc How can i do this 这有效,但我需要更改客户地址,手机号码等,我该怎么做

Why regex, a simple String.Replace will do the job: 为什么使用正则表达式,一个简单的String.Replace可以完成此工作:

string oldText = File.ReadAllText(path);
string newText = oldText.Replace("@CoustomerName", objProposar.FirstName);
// other ...
File.WriteAllText(path, newText);

If your file is XML - use XML way of doing it, like XDocument, otherwise string.Replace is a better option: 如果您的文件是XML,请使用XML方式,例如XDocument,否则使用string.Replace是更好的选择:

string readfile = File.ReadAllText(path);
readfile = readfile.Replace("@CoustomerName", objProposar.FirstName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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