0

I am creating xml file using below code problem is when there is extra spaces in sentence or content this code is converting it into single space how to handle this

    using System;
using System.IO;
using System.Xml;

public class Sample
{
    public static void Main()
    {
        // Create the writer.
        XmlTextWriter writer = null;
        writer = new XmlTextWriter("c:\\ws.html", null);

        // Write an element (this one is the root).
        writer.WriteStartElement("p");

        // Write the xml:space attribute.
        writer.WriteAttributeString("xml", "space", null, "preserve");

        // Verify that xml:space is set properly. 
        if (writer.XmlSpace == XmlSpace.Preserve)
            Console.WriteLine("xmlspace is correct!");

        // Write out the HTML elements.  Insert white space 
        // between 'something' and 'Big'
        writer.WriteString("something    extra spacess in this line but not     coming in xml    i want this extra spaces");
        writer.WriteWhitespace("  ");
        writer.WriteElementString("b", "B");
        writer.WriteString("ig");

        // Write the root end element.
        writer.WriteEndElement();

        // Write the XML to file and close the writer.
        writer.Close();
    }
}
Yogesh
  • 150
  • 1
  • 4
  • 20

1 Answers1

0

Have you tried writer.WriteCData()? That will surround it with a <![CDATA[...]]> block that may preserve spaces (haven't tried it myself, so couldn't say).

Obviously, not great if you don't want it as a data block.

tristankoffee
  • 670
  • 7
  • 8