Saturday, 26 May 2018

XMLformatting in c#

Format the minify XML into readable way which help to view it properly and easy to read in C#. You can make your simple xml formatter by using the below few lines of code in C#.
static void Main(string[] args)
                string xml = @"<?xml version='1.0' encoding='UTF - 8' ?><employees><employee><id>1</id><firstName>Leonardo</firstName>
            </employee></employees> ";

                XDocument doc = XDocument.Parse(xml);
                Console.WriteLine("Input will be {0} \n", xml.ToString());
                Console.WriteLine("Output will be {0} \n" , doc.ToString());
            catch (Exception)
                // Handle and throw if fatal exception here; don't just ignore them
               // return xml;

Below line is only useful which will parse the string into proper XML Format

XDocument doc = XDocument.Parse(xml);

You can see the output as like below. XML Format

No comments:

Post a Comment