通过使用 XML 而不是 ServerManager 类,我能够自己弄清楚。这几乎是我想要的。
XmlDocument doc1 = new XmlDocument();
doc1.Load(@"web.new.config");
XmlDocument doc2 = new XmlDocument();
doc2.Load(@"web.config");
XmlNode rules = doc2.SelectSingleNode("/configuration/system.webServer/rewrite/rules");
XmlNodeList baserules = doc2.SelectNodes("/configuration/system.webServer/rewrite/rules/rule[contains(@name, 'GenericPrefix')]");
XmlNodeList ruleList = doc1.SelectNodes("/configuration/system.webServer/rewrite/rules/rule[contains(@name, 'GenericPrefix')]");
foreach(XmlNode baseruleOld in baserules)
{
baseruleOld.ParentNode.RemoveChild(baseruleOld);
}
foreach(XmlNode rule in ruleList)
{
XmlNode tocopynode = doc2.ImportNode(rule, true);
rules.AppendChild(tocopynode);
}
doc2.Save("web.config");