How to convert C# Code to a XML file to parse it

R

Robert Fischbacher

Hello,

does anybody know if there is a existing tool that can convert valid
C# Code to a XML structure? I tried to parse a C# program to analyze
the structure of the existing program code, but it's really, really
very complicated. So, if there would be a existing tool that converts
the C# file to XML it should be easier to parse then the XML file.
Because the CreateParser method of the CodeDomProvider class does not
work, I now try to find an other method to parse the C# code.

Do you have any ideas? Thanks.

Greetings

Robby
 
N

Noah Coad [MCP & MVP]

If you need to save/load state information to an XML file, this will work:

Serializes an object's state information (properties) to an XML file:

XmlSerializer ser = new XmlSerializer(typeof(MyClass));
TextWriter write = new StreamWriter("MyClass.xml");
ser.Serialize(write, this);
write.Close();

Deserializes an XML file and restores state information to a class:

XmlSerializer ser = new XmlSerializer(typeof(MyClass));
TextReader reader = new StreamReader("MyClass.xml");
MyClass mc = (MyClass) ser.Deserialize(reader);
reader.Close();

Perhaps this is what you were looking for. If not, please elaborate on what
you are attempting to do.

-Noah Coad
Microsoft MVP & MCP
 
R

Robert Fischbacher

Hello Noah Coad,

first of all: Thank you.
But I don't want to serialize a object during runtime,
I want to convert a C# file into a XML file. Here is a
example what I plan to do:

This is my C# file:
--------------------
using System;
class CRobby {
static void Main() {
for (int i=0;i<10;i++) {
Console.WriteLine("Hello");
Console.WriteLine("Robby");
}
}
}

Now, I want to get the XML structure of this code, for example:
--------------------
<command>using System;</command>
<class name="CRobby">
<function command="static void Main()">
<block command="for (int i=0;i<10;i++)">
<command>Console.WriteLine("Hello");</command>
<command>Console.WriteLine("Robby");</command>
</block>
</function>
</class>

I need to analyze the structure of a C# source and I thought,
it should be easier to parse a XML file than a C# source.
The CreateParser method of the CodeDOM seems to do that, but
it always returns Null. I think, internal the C# Compiler
uses the CreateParser method, but MS does not offer the
functionality to us public programmers.

Greetings from Bavaria
Robby
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top