tags:

views:

32

answers:

1

I want to parse a simple XML Doc and create simple syntax coloring...

<parent>
   <child>Value</child>
   <child>
      <grandchild>Value2</grandchild>
   </child>
</parent>

So all the < and > are blue The node names are red and the value is black.

I was wondering if anyone had a good regex do this? Right now I am using a forloop and going character by character.... don't really think that is the best way so looking for ideas.

My XML will be simple, won't ever have attributes, just a simple pattern like the above example. I would like to convert it to html.

Ideas?

+2  A: 

You do not mention how you are rendering the output. But As a starting point, rather than parse the document character by character, you could use an XmlTextReader to read the XML nodes and then just emit the appropriate HTML output when you hit each node.

For example when you reach an element node, you can write out the '<' with a CSS style that renders in blue, node.Name in red and '>' in blue, all other node type you write out in black. So the parsing is handled by the XmlTextReader and you are simply responsible for the presentation.

Chris Taylor
How do I maintain indentation?
gmcalab
The reader can return white space nodes which you can then write the appropriate html to present the whitespace. http://msdn.microsoft.com/en-us/library/system.xml.xmlnodetype.aspx
Chris Taylor