C # or javascript code formatting

I am currently using Highlighter Syntax to display XML or SOAP messages on a page. This is great for messages that are already formatted correctly (line breaks, indentation, etc.). But if I had an XML string, for example:

string xml = "<doc><object><first>Joe</first><last>Smith</last></object></doc>"; 

I would write a line on the page, and the javascript shortcut would correctly syntax highlight the line, but everything would be on the same line.

Is there a C # line formatter or some syntax highlighting library that has a smart indent function that will insert line breaks, indentation, etc. ??

+4
source share
1 answer

Since this is a line, adding line breaks and indentation will change the actual value of the xml variable, which is not what you want your code formatter to do!

Note that you can format XML in C # before writing to the page, for example:

 using System; using System.IO; using System.Text; using System.Xml; namespace XmlIndent { class Program { static void Main(string[] args) { string xml = "<doc><object><first>Joe</first><last>Smith</last></object></doc>"; var xd = new XmlDocument(); xd.LoadXml(xml); Console.WriteLine(FormatXml(xd)); Console.ReadKey(); } static string FormatXml(XmlDocument doc) { var sb = new StringBuilder(); var sw = new StringWriter(sb); XmlTextWriter xtw = null; using(xtw = new XmlTextWriter(sw) { Formatting = Formatting.Indented }) { doc.WriteTo(xtw); } return sb.ToString(); } } } 
+2
source

Source: https://habr.com/ru/post/1302483/


All Articles