我要转换的配置如下所示:
<sdfsdfsd>
<blah>
<mypath>D:\my\old\path\aaa</mypath>
</blah>
<blah>
<mypath>D:\my\old\path\bbb</mypath>
</blah>
<blah>
<mypath>D:\my\old\path\ccc</mypath>
</blah>
</sdfsdfsd>
我只想替换 D:\my\old\path\<unique value>
与 D:\my\new\path\<unique value>
我只看到示例替换 <><> 之间的完整值或 <> 内的属性。我只想在文件中的任何地方做一个简单的字符串替换,这可能吗?
试试 xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<sdfsdfsd>" +
"<blah>" +
"<mypath>D:\\my\\old\\path\\aaa</mypath>" +
"</blah>" +
"<blah>" +
"<mypath>D:\\my\\old\\path\\bbb</mypath>" +
"</blah>" +
"<blah>" +
"<mypath>D:\\my\\old\\path\\ccc</mypath>" +
"</blah>" +
"</sdfsdfsd>";
XElement element = XElement.Parse(xml);
foreach(XElement mypath in element.Descendants("mypath"))
{
mypath.SetValue(((string)mypath).Replace("old","new"));
}
}
}
}
我是一名优秀的程序员,十分优秀!