How to specify a "Custom Tool" for transforming a file in VS2010?
The Properties window in VS2010 for most file types (eg .cs, .xml, .xslt) allows one to specify a custom tool for transforming files. For reference, here is the tool tip that one gets when selecting the "Custom Tool" field.
Specifies the tool that transforms a file at design time and places the output of that transformation into another file. For example, a dataset (.xsd) file comes with a default custom tool.
I am looking for information as to how to set and use this property.
Here is the problem I am trying to solve. I am transforming and XML file by applying XSLT. I am using extension objects during the transformation as described here.
In doing so I have rendered Visual Studio useless as a tool for editing and debugging my XSLT. I am hoping that I can write a simple transformation engine that will allow us to use Visual Studio as we do for XSLT documents that do not use extension objects. I think (hope) that the Custom Tool property is the key to making this work.
TIA
Debugging XSLT transformations with extension functions with the ability to have breakpoints in both the XSLT code and the extension functions code has been supported since VS2005 .
Just use this XslCompiledTransform constructor overload .
Parameters enableDebug Type: System.Boolean true to generate debug information; otherwise false. Setting this to true enables you to debug the style sheet with the Microsoft Visual Studio Debugger.
Remarks
The following conditions must be met in order to step into the code and debug the style sheet:
The enableDebug
parameter is set to true.
The style sheet is passed to the Load
method either as a URI, or an implementation of the XmlReader
class that implements the IXmlLineInfo
interface. The IXmlLineInfo
interface is implemented on all text-parsing XmlReader
objects.
In other words, if the style sheet is loaded using an IXPathNavigable
object, such as an XmlDocument
or XPathDocument
, or an XmlReader
implementation that does not implement the IXmlLineInfo
interface, you cannot debug the style sheet.
The XmlResolver
used to load the style sheet is a file-based XmlResolver
, such as the XmlUrlResolver
(this is the default XmlResolver
used by the XslCompiledTransform
class).
The style sheet is located on the local machine or on the intranet.
Here is a small code example :
// Enable XSLT debugging.
XslCompiledTransform xslt = new XslCompiledTransform(true);
// Load the style sheet.
xslt.Load("output.xsl");
// Create the writer.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent=true;
XmlWriter writer = XmlWriter.Create("output.xml", settings);
// Execute the transformation.
xslt.Transform("books.xml", writer);
writer.Close();
A workaround to debug XSLT files that use extension objects is to create a test .exe file project (such as a Console Application project), and call the XSLT (with all extension objects correctly referenced) from that project.
You can set breakpoints, then "Start Debugging" the test .exe project to step into the XSLT file.
Prior to 2010, the "Custom Tool" property specified the name of a class that was registered with VS as a processing tool that would take the value of the project item and produce more files. For this to work you had to write the tool class, compile it to an assembly, and register that assembly with Visual Studio. An example of this process can be found here, but do note it is not specifically for VS 2010 and so may break -- the custom tools are based on the COM object model for VS, apparently.
I'm not sure how well-supported this feature remains in VS 2010. I don't seen any (obvious) documentation on writing these tools in the VS 2010 SDK and this post on the Visual Studio blog appears to discuss the feature (under "Custom Build Tools") or a similar one, but I've never seen any of the UI it described in my copy of VS 2010.
In any case, if you're just trying to run some stuff through a command line, you may be better off just editing the MSBuild of your .csproj to use an <Exec> task
-- if you do want to build a custom tool, my first link should hopefully at least get you started.
上一篇: 有关运算符重载的问题