DavidColson
DavidColson

Reputation: 8587

Code writing using Xml

This may seem an odd question but hear me out. I was looking through the files of the NeoAxis engine when I found an xml file that had what appeared to be lines of code stored in strings.

Due to the engine being closed source I cannot see how it works but to all experienced devs out there, how is this possibly working?

If the XML was loaded into the code the string would be stored as a string. But is there any way, in a language such as c++ or c# to use string as statements in a program?

I dont know if Im allowed to show you this XML file but the statements were prefixed with a letter and a colon. Like this: "M:Class.DoSomething();".

If I can get some idea of how this is done it would be very useful for alot of things

Upvotes: 0

Views: 86

Answers (4)

Michael Kay
Michael Kay

Reputation: 163262

XML can be used to store anything, including fragments of program text. There are many ways the consumer of the XML might use such fragments. It might be able to execute them interpretively, or it might simply use them to construct a complete program which is then compiled and executed in the usual way.

The semicolon in the code fragment you posted suggests that it is probably not XPath, but apart from that it could be. Storing XPath expressions in an XML document is very common practice.

Upvotes: 0

Moog
Moog

Reputation: 10193

If you really want to know how this works for .NET then you can check out Rick Strahl's excellent tutorial on Dynamic Code Execution in .NET. It's a bit old now but the principle is still applicable

Upvotes: 0

Kris Vandermotten
Kris Vandermotten

Reputation: 10201

That M:Class.DoSomething() syntax looks a lot like it may come from an XML documentation file generated by the compiler.

See for example http://msdn.microsoft.com/en-us/library/aa288481(v=VS.71).aspx

Upvotes: 0

NominSim
NominSim

Reputation: 8511

Yes, just use the built in Code Compiler with your String source code:

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();

System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters,SourceString);

You can read more about it here or here.

Upvotes: 2

Related Questions