Using XslCompiledTransform in C# : xlst file compared to compiled dll - c#

We're developing a program that generates DOCX files using XML and transforms. Currently one person is responsible for development of the XML and XSLT files and another for the C# program that puts it all together. I'm the one developing the C# side. Using the .xslt file the transform works fantastic. The issue I'm having is that the manager of this project doesn't want me to distribute the .xslt's. Instead I'm compiling the file into a dll assembly. No I'm getting errors such as:
An error occurred while loading document ". See InnerExeption for a complete description of this error. This operation is not supported for a relative URI.
I know it's probably something simple I've probably missed. Does anyone know of a good article that outlines the implementation difference between using the xlst file and a compiled dll.
Thanks.

Related

Find out what packages, classes and methods the Jar file contains

I have a Java code editor with syntax highlighting, with a code hint menu and quick actions. The editor is written in C#. Once again, this editor is for editing Java code! I need to know what packages, classes, methods are in the Jar library (Jar file) for further
code hinting and syntax highlighting.
I have an idea about decompiling the Jar to find out "its contents", but I suspect that there is another more efficient way to implement this.
This is what my editor looks like:
It turned out to be relatively simple. I found the 'jd-gui' repository (https://github.com/java-decompiler/jd-gui), here is the app: (http://java-decompiler.github.io/). The program itself is written in java and is a very good java decompiler based on the jd-core core (https://github.com/java-decompiler/jd-core).
I liked this application, because. it implements a detailed overview of the entire decompiled application (you can see what packages, classes, methods, fields are in the jar), that's exactly what I needed! Because it was an open source repository, I just tweaked it a little, and now when decompiling, the program saves the xml file with the "contents of the jar file". And then I wrote an xml parser in c# to load data and further work with them. That's all...
Code with edited jd-gui saving xml file with jar content: https://drive.google.com/drive/folders/1_opg0BBIrmHoZ125F0Fyl9n_0o4xutiL?usp=sharing When loading a jar file, next to it (in the same folder) an xml file with the contents of this file will be saved.

Check xslt file compilation

How i can compile my xslt file ? this problem face me to check if the file already compiled to check error free or not? when try to use xsltc
'xsltc' is not recognized as an internal or external command,
operable program or batch file.
My xsl file contain include tage to another xsl file so some online compiler doesn't serve my problem.
There is any way to make that to convert xsl to assembly file?
'xsltc' is not recognized as an internal or external command, operable program or batch file.
The program xsltc is a commandline program. Add it to your path, or to the directory where you are executing it, or select the path in your command. The result of compiling with xsltc is what they call a translet, a set of classes, which you can use in Java to add to your project.
You tagged your question with C#, you can use XslCompiledTransform in C# to get compile first, and then transform. This will show you whether you have any errors. The MSDN has a couple of examples on how to do this.
There is any way to make that to convert xsl to assembly file?
With Saxon, you can do this in Java, and in .NET you can use XslCompiledTransform.CompileToType and subsequently load it with XslCompiledTransform.Load(Type), also, we are planning to implement it for Exselt, which is a .NET XSLT 3.0 processor.
The typical way XSLT is used within programs is to simply load and compile on program startup (this usually takes no more than a few ms anyway) and then transform multiple times. Whether you do it this way in Java or C# does not matter much. In C# the compiled transform object is thread-safe and can be called with Transform simultaneously from multiple threads.
This approach has the advantage that you do not need to recompile your source if your XSLT changes.
Update (xsltc.exe)
Did you mean Microsoft's xsltc.exe? You can resolve your error above by simply opening a Visual Studio command prompt from your start menu. It will automatically set the correct paths.
After you have compiled your assembly, you can load it by following this MSDN article guideline.

XmlSerializers with addin outlook

I’m trying to create add-in for outlook and have issue with deserialization and antivirus program.
I’ve noticed that when my add-in tried to deserialize any data, .NET framework created temporary dll in “C:\Users\{UserName}\AppData\Local\Temp\" folder.
This dll existed very short time, but from time to time antivirus locked it and add-in thrown error message that file is used by another process.
I’m tried to get rid of temporary dll and found recommendations to use sgen tool for creation of XmlSerializers.dll.
I generated MyAssembly. XmlSerializers.dll with strong name and placed it to the folder with add-in (C:\Program Files (x86)\MyAddin). But it doesn’t help.
Then I tried to place MyAssembly. XmlSerializers.dll to GAC and then to outlook folder, but had no success. When dll was called from GAC I got following error message, but dll has no any reference.
"System.IO.FileNotFoundException: Could not load file or assembly or
one of its dependencies. The system cannot find the file specified."
Please add any thoughts how can I to get rid of temporary dll
When an XmlSerializer for a type is first constructed, internally the XML serialization engine generates c# code to serialize and deserialize the type, writes it to %TEMP% in temporary file(s), then compiles and loads the resulting assembly, finally deleting any temporary files. (Subsequent usages of XmlSerializer reuse the created assembly, see here for details.)
It looks as though your antivirus software is being ultra-aggressive at scanning any "unexpected" files created by the Outlook process, immediately trapping the creation of the file and scanning the results. While under most circumstances this would seem praiseworthy, it clearly conflicts with Microsoft's design for XmlSerializer.
So, what to do about this? You have several options:
Switch to DataContractSerializer, which does not use this architecture. DataContractSerializer is less flexible that XmlSerializer, however, and may not be able to parse your XML without preprocessing it.
Enable precompiled serialization assemblies. It's not sufficient to simply set GenerateSerializationAssemblies = On as is specified in the documentation, you have to jump through several hoops to make this actually work. See answers to Generating an Xml Serialization assembly as part of my build for ways to do it. I was able to make the accepted answer work with my old Visual Studio 2008 by editing my project file as described and then removing the Platform="$(Platform)" attribute. You might need to tweak the answer differently for your VS version.
After actually enabling pre-generated serialization DLLs I verified with Process Monitor that no files were written to %TEMP% when deserializing XML in a test console application.
See also here: Boost performance with Pre-generated XmlSerializers.
Update
After a bit of testing, I found that, if your root object does not exist in the assembly you are building, or if is a generic collection like List<T> or T [], then precompiled serializer assemblies are not used. However, making a non-generic subclass of List<T> re-enables serializer assemblies, e.g. public class RootObjectList : List<RootObject> { }.
For more, see here: All about XmlSerializer Performance and Sgen.
Convert your XML to JSON with Json.NET, then deserialize the JSON.
If your XML is simple, you could load it into an XDocument and query it with Linq to XML.

Why doesn't C# have header files? Will the namespace take care of everything?

Can anyone tell clearly about the usage of header files and namespaces in C#?
Because in C++ I was using ******.h files to read library functions. And when I saw some sample programs in C# they were missing, Can anyone tell me why?
I'm using C# to develop a custom tool for a CAD application. Whenever I use the appropriate function to open the file (CAD file), the compiler is giving me an error stating that the function names which I supply are not available in the context. Here what does meant by context?
When I opened the help file of that CAD application the function which is responsible for opening the file has bee mentioned under a header file called uf_part.h. But there is an namespace called NXOpen.
I used the namespace as using NXOpen in Visual Basic, isn't that enough? DO I need to supply that header file as well? If so, how?
C# is more "programmer friendly". When dealing with files of the same project, instead of manually specifying "header file" every time, it will go and look in all the project files for a match according to the namespace.
To understand this, do the following steps:
Start new project in Visual Studio. (No matter what type, WinForms or Console)
Right click the project and add new class.
In your main class note you can see the new class you just added, without adding any header.
How this is done? Simply by having the same namespace to both classes. The .NET engine is smart enough to link all those classes together.
Now, when it comes to external code meaning code sitting in a different DLL file the trick is to add reference to that DLL (in Studio --> Right click project --> Add reference --> Browse) then you need to specify you are going to use that DLL by adding a using statement on top:
using ExternalDllName.ExternalNamespace;
That's about it. Unlike C++ you don't need to have .h file as .NET will automatically search the referenced DLL files for a match.
There's no such thing as header file in .net, because all needed metadata is contained in referenced assembly itself.
Have you referenced needed assembly in you project?
Also please mind that there's no such thing as "function" in C#, only class methods (which means that you have to specify object or static class in you call).
Also: General Structure of a C# Program
Compilers for modern languages, such as C# or Java store within compiled files information on the classes and methods they contain, and this information can be used to check the correctness of calls made from one source file to another or to library classes.
When C was invented disk space, memory and CPU power were precious resources and this approach would not have been possible. Header files were introduced to allow the compiler to check that different source files conformed to the same interface. When C++ was invented the approach described above could have been possible, but I guess that it was chosen to stick to the C one for compatibility reasons.

Creating extra type definitions at compile time

Recently I've been working with MSTest, and I noticed that the testframework generates accessor classes dynamically at compile time. How can one do this?
There's an xml file in a VS2010 C# project. I'd like to make an enum out of certain data in this xml file. Can this be done? And if so, how?
I'd recommend T4 templates myself. Very easy to use and specifically designed to allow you to generate code during the build. http://msdn.microsoft.com/en-us/library/bb126445.aspx
Method A) Read the xml file, parse it, generate C# code from it, write out the C# code to a temp file, compile that code; delete the temp file.
Method B) Read the xml file, parse it. Generate IL code directly from it using method in the System.Reflection.Emit namespace, or those in the System.CodeDom namespace.
MSTest achieves this in a couple of different ways. In short they essentially do the following IIRC
Hook into the build system
At the start of the build they generate their acessor's into hidden files in the project
After the build completes they remove their files
You can achieve a similar effect via the same process. However hooking into the build system is a bit complicated. A much simpler approach is to build a custom tool / code generator and hook. This allows you to process a file at build time and spit out a corresponding code file to include in the build.
There are several examples on the web on how to achieve this. Here are a couple
http://www.raboof.com/Projects/VsCodeGeneratorShim/
http://www.ramymostafa.com/?p=204
The System.CodeDom Namespace is one option you have.
It allows you to automatically generate a class using C# Code and compile it as well.
You can maybe call this code as a postbuild during your build of your project.
This example shows how to create a class using this namespace

Categories