是否可以在C#项目中使用XDT?

Rig*_*iga 2 c# xslt xdt-transform

我在数据库表中有XML需要转换更新值,简单的更改,具体取决于某些条件.

我的研究但是只找到了适用于Web.Config或App.Config的工具/插件:

http://ctt.codeplex.com/

http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

我可以使用XSLT,但XDT似乎更理想,更简单,但我如何在我的C#项目中使用它?

谢谢

Bra*_*tie 6

对于遇到这篇文章的人来说,有一个NuGet包提供了执行这种转换的能力:

安装包Microsoft.Web.Xdt

然后,它是这样的:

// Some example file paths
var sourceDoc = "web.config";
var transDoc = "web.Debug.config";
var destDoc = "bin\web.config";

// The translation at-hand
using (var xmlDoc = new XmlTransformableDocument())
{
  xmlDoc.PreserveWhitespace = true;
  xmlDoc.Load(sourceDoc);

  using (var xmlTrans = new XmlTransformation(transDoc))
  {
    if (xmlTrans.Apply(xmlDoc))
    {
      // If we made it here, sourceDoc now has transDoc's changes
      // applied. So, we're going to save the final result off to
      // destDoc.
      xmlDoc.Save(destDoc);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当然,这是非常基本的,只需要很少的检查,但它为您提供了要点.