如何用 XDT 转换替换字符串
How can I replace a string with XDT transforms
我要转换的配置如下所示:
<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\new\path\<unique value>
替换D:\my\old\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"));
}
}
}
}
我要转换的配置如下所示:
<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\new\path\<unique value>
D:\my\old\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"));
}
}
}
}