2016-04-19 19 views
0

我想讀取包含閱讀文本文件,並進行更改C#

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

一個文本文件替換該@CoustomerName與Coustomer名稱通行證在運行時

到那時,我用這個

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

這工作但我需要在Coustomer地址,手機號碼等的變化 我該怎麼辦

+1

它看起來你有* XML *,如果是你的情況,爲什麼不使用'System.Xml.Linq.XDocument'? –

回答

1

爲什麼正則表達式,簡單String.Replace將做的工作:

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

我使用這個,但不工作..... –

+0

@MukulKhatter:有趣的是,發生了什麼呢? –

+0

它的工作原理.... 從我身邊的語法錯誤:P –

0

如果你的文件是XML - 這樣做,像的XDocument的使用XML的方式,否則string.Replace是一個更好的選擇:

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