只是为了扩展一点。我需要上面讨论的内容。只能运行转换。然后将其连接到我的构建过程中,在我的例子中恰好是 TeamCity。
您将需要使用 Microsoft.Web.Publishing.Tasks,您可以使用 Nuget 将其粉碎。好吧,我在 VS2013 中,所以我可以。我敢肯定,否则你可以获取 dll。
写了一个简单的控制台应用程序。您可能会发现它很有用。
程序.cs
using System;
namespace WebConfigTransform
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 3)
{
Console.WriteLine("Config Gen ... usage -source -transform -destination");
Environment.Exit(-1);
}
Transform t = new Transform(args[0], args[1], args[2]);
t.Run();
}
}
}
变换.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Security;
using System.Security.Permissions;
using Microsoft.Web.XmlTransform;
namespace WebConfigTransform
{
class Transform
{
private readonly string m_source;
private readonly string m_transform;
private readonly string m_destination;
public Transform(string source, string transform, string destination)
{
m_source = source;
m_transform = transform;
m_destination = destination;
}
private void TransformFiles()
{
var xmlTarget = new XmlTransformableDocument();
xmlTarget.PreserveWhitespace = true;
xmlTarget.Load(m_source);
var xmlTransform = new XmlTransformation(m_transform);
if (xmlTransform.Apply(xmlTarget))
xmlTarget.Save(m_destination);
else
{
Console.WriteLine("Unable to apply transform.");
Environment.Exit(-1);
}
}
private void CheckPermissions()
{
string directoryName = m_destination;
PermissionSet permissionSet = new PermissionSet(PermissionState.None);
FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.Write, directoryName);
permissionSet.AddPermission(writePermission);
if (!(permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet)))
{
Console.WriteLine("Cannot write to file : " + m_destination);
Environment.Exit(-1);
}
}
private void CheckFileExistance()
{
List<string> ls = new List<string>();
ls.Add(m_source);
ls.Add(m_transform);
foreach (string item in ls)
{
if (!File.Exists(item))
{
Console.WriteLine("Cannot locate file : " + item);
Environment.Exit(-1);
}
}
}
public void Run()
{
CheckFileExistance();
CheckPermissions();
TransformFiles();
}
}
}