0

我正在使用下面的代码创建 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();
    }
}
4

1 回答 1

0

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

显然,如果您不希望它作为数据块,那就不好了。

于 2013-02-12T13:22:33.433 回答