Gabe
Gabe

Reputation: 50503

Parse XML and apply syntax color

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?

Upvotes: 0

Views: 572

Answers (1)

Chris Taylor
Chris Taylor

Reputation: 53709

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.

Upvotes: 2

Related Questions