2014-10-07 98 views
0

我们每年发布几次我们产品的更新,我们需要能够对客户的web.configs进行更改。web.config文件转换

我们希望能够通过c#应用这些更改。

有没有人使用过ctt.exe程序并为它实现了一个包装类?在最初的测试中,它似乎去除了所有不理想的空白。

+0

你每位顾客每次发布更新web.config文件或只有一个为所有客户的web.config? – artm 2014-10-07 11:26:30

+0

每个版本都可能有变化,客户可能已经修改了web.config的部分,因此我们需要根据Web配置转换目标。 – Tim 2014-10-07 11:32:30

+0

您看过slowcheetah吗? http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5另外我假设你知道关于web.config.release – artm 2014-10-07 11:38:50

回答

2

找到了Microsoft.Web.XmlTransform库,它可以在不需要第三方工具的情况下完美解决这个问题。

using Microsoft.Web.XmlTransform; 

...

using (XmlTransformableDocument document = new XmlTransformableDocument()) 
       using (XmlTransformation transformation = new XmlTransformation(transformFilePath)) 
       { 
        document.PreserveWhitespace = true; 
        document.Load(sourceFilePath); 

        var success = transformation.Apply(document); 
        if (!success) 
        { 
         string message = string.Format(
          "There was an unknown error trying while trying to apply the transform. Source file='{0}',Transform='{1}', Destination='{2}'", 
          sourceFilePath, transformFilePath, destinationFilePath); 
         throw new Exception(message); 
        } 

        document.Save(destinationFilePath); 
       }