Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use XDT inside C# projects?

I have XMLs in database table that need to transform updating values, simple changes, depending on certain conditions.

Did my research but only found tools/plug-ins to apply to Web.Config or App.Config:

http://ctt.codeplex.com/

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

I could use XSLT but XDT seems ideal, simpler, but how can I use it inside my C# project?

Thanks

like image 620
Riga Avatar asked Nov 25 '25 06:11

Riga


1 Answers

For anyone coming across this post, there is a NuGet package providing the ability to perform this transformation:

Install-Package Microsoft.Web.Xdt

Then, it's something along the lines of:

// 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);
    }
  }
}

That is, of course, very basic with minimal checking, but it gives you the gist.

like image 200
Brad Christie Avatar answered Nov 27 '25 20:11

Brad Christie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!