Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML serialize Roslyn SyntaxTree?

I'm looking for options to convert C# to XML and also deserialize back to C# again. Anyone know of any existing solutions for this? I assume a Roslyn SyntaxTree is not Xml-serializable. I also assume making a xml-serializer for a syntaxtree would be quite a large undertaking. But maybee not?

Why do I need this? I want to generate code from an xml-model via xsl to syntaxtree-xml and then to c# (or maybee vb).

I don't want to generate C# directly from XSL (can be done but I don't feel it's ideal). I need XSL because I'm also going to generate XAML and HTML from the XML-model and I'd like to also be able to generate any code behind needed etc.

I know about T4-templates but right now I'm looking at other options for various reasons.

like image 434
Andreas Zita Avatar asked Oct 21 '22 17:10

Andreas Zita


1 Answers

Roslyn is an open compiler. I assume you can straightforwardly insert your own code to walk over the AST and dump it in XML format. Given that it is open, presumbly you can implement your own extension to read your dumped XML, and build a Roslyn AST (which I think can then be prettyprinted). That seems like a lot of work.

For a canned solution, see C# AST in XML. A similar read-XML-to-make-AST would be needed for that solution, too.

You might not want to use XML for trees; such files are generally enormous. See What would an AST (abstract syntax tree) for an object-oriented programming language look like?.

It is likely better for you to operate within the context of a tool that parses and builds ASTs, do your manipulation directly on those ASTs, and then prettyprint the result. Our DMS system has both parsing (shown by the previous two examples) and prettyprinting for many languages including C# already built-in, and many useful APIs for inspecting/modifying/generating code from the ASTs it constructs. (XSL is lot less useful for analyzing ASTs than you might think; it doesn't understand "context" [e.g., the surrounding scopes and declarations] that give programs the meaning they have).

like image 56
Ira Baxter Avatar answered Nov 15 '22 05:11

Ira Baxter