Beautify XML to print in C#

You can print XML normally but it will not be formatted, we have solution which can help you to output XML in nice format We will use XmlTextWriter function to do this here is simple function which you can use in project.

public static String BeutyXML(String XML)
        {
            String Result = "";
 
            MemoryStream mStream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mStream, Encoding.Unicode);
            XmlDocument document = new XmlDocument();
 
            try
            {                
                document.LoadXml(XML);
                writer.Formatting = Formatting.Indented;
 
                document.WriteContentTo(writer);
                writer.Flush();
                mStream.Flush();
 
                mStream.Position = 0;
 
                StreamReader sReader = new StreamReader(mStream);
                String FormattedXML = sReader.ReadToEnd();
 
                Result = FormattedXML;
            }
            catch (XmlException)
            {
            }
 
            mStream.Close();
            writer.Close();
            
            // Return formatted XML
            return Result;
        }

you can call it with simple command

textBox2.Text = BeutyXML(payout.Getsoapresponse());

That’s all!

Leave a Reply

Your email address will not be published. Required fields are marked *