使用c#的xml空白问题

时间:2013-02-12 13:17:10

标签: xml xmltextwriter

我正在使用下面的代码创建xml文件问题是当句子或内容中有额外的空格时,这段代码将它转换为单个空格如何处理这个

    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();
    }
}

1 个答案:

答案 0 :(得分:0)

你试过writer.WriteCData()吗?那将用<![CDATA[...]]>块围绕它,可以保留空间(我自己没有试过,所以不能说)。

显然,如果你不想将它作为数据块,那就太好了。