Reputation: 4822
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://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
Upvotes: 3
Views: 1798
Reputation: 101604
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.
Upvotes: 7
Reputation: 4822
At the end found a nice piece of code that does what I wanted:
http://petemontgomery.wordpress.com/2010/09/20/microsoft-xdt-language/
Upvotes: 0