不间断空格的序列化选项 [英] Serializing options for non-breaking spaces

查看:28
本文介绍了不间断空格的序列化选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文档,我想加载它,然后将其保存回磁盘.其中一个元素具有包含不间断空格   的文本.当我将文档序列化回来时,它会作为一个不间断的空间出现,而不是像原始代码那样的代码点.此问题还会影响其他代码点,例如 &",但不会影响 <> 因为后者在元素中是不合法的.您甚至不需要操作文档来使其替换字符.

I have an XML document, that I want to load manipulate and then save back to disk. One of the elements has text that contains a non-breaking space   in it. When I serialize the document back it comes out as a non-breaking space but not the code point like it was in the original. This problem also effects other code points like & and ", but not < and > since the later would not be legal in elements. You don't even need to manipulate the document to cause it to replace the characters.

有没有办法让它作为代码点序列化回来?文档示例

Is there any way to get it to serialize back as the code point? Document example

<Node>
<InnerNode>Some&#160;Text</InnerNode>
</Node>

代码示例

XmlDocument doc = new XmlDocument {PreserveWhitespace = true};
doc.Load(fileStream.BaseStream);
doc.Save(path);

推荐答案

您的目标可以通过使用 修饰的XmlWriter 对象.下面的代码用 WriteString 方法中的 XML 实体 &#160; 替换了不间断空格.

Your goal can be achieved by using a decorated XmlWriter object. The code below replaces non-breaking spaces with the XML entity &#160; in the WriteString method.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader fileStream = new StreamReader("input.xml");
            string path = "output.xml";

            XmlDocument doc = new XmlDocument { PreserveWhitespace = true };
            doc.Load(fileStream.BaseStream);

            // Do some processing...

            MyXmlWriter writer = new MyXmlWriter(XmlWriter.Create(path));
            doc.Save(writer);
        }
    }

    public class MyXmlWriter : XmlWriter
    {
        private readonly XmlWriter writer;

        public MyXmlWriter(XmlWriter writer)
        {
            if (writer == null) throw new ArgumentNullException("writer");
            this.writer = writer;
        }

        // Output non-breaking space as character entity
        public override void WriteString(string text)
        {
            string[] parts = text.Split((char)160);
            for (int i = 0; i < parts.Count(); i++)
            {
                this.writer.WriteString(parts[i]);
                if (i + 1 < parts.Count())
                    this.writer.WriteRaw("&#160;");
            }
        }

        // The rest of the XmlWriter methods implemented using Decorator Pattern
        public override void Close()
        {
            this.writer.Close();
        }

        public override string LookupPrefix(string ns)
        {
            return this.writer.LookupPrefix(ns);
        }

        public override void Flush()
        {
            this.writer.Flush();
        }

        public override WriteState WriteState
        {
            get { return this.writer.WriteState; }
        }

        public override void WriteBase64(byte[] buffer, int index, int count)
        {
            this.writer.WriteBase64(buffer, index, count);
        }

        public override void WriteRaw(string data)
        {
            this.writer.WriteRaw(data);
        }

        public override void WriteRaw(char[] buffer, int index, int count)
        {
            this.writer.WriteRaw(buffer, index, count);
        }

        public override void WriteChars(char[] buffer, int index, int count)
        {
            this.writer.WriteChars(buffer, index, count);
        }

        public override void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            this.writer.WriteSurrogateCharEntity(lowChar, highChar);
        }

        public override void WriteWhitespace(string ws)
        {
            this.writer.WriteWhitespace(ws);
        }

        public override void WriteCharEntity(char ch)
        {
            this.writer.WriteCharEntity(ch);
        }

        public override void WriteEntityRef(string name)
        {
            this.writer.WriteEntityRef(name);
        }

        public override void WriteProcessingInstruction(string name, string text)
        {
            this.writer.WriteProcessingInstruction(name, text);
        }

        public override void WriteComment(string text)
        {
            this.writer.WriteComment(text);
        }

        public override void WriteCData(string text)
        {
            this.writer.WriteCData(text);
        }

        public override void WriteEndAttribute()
        {
            this.writer.WriteEndAttribute();
        }

        public override void WriteStartAttribute(string prefix, string localName, string ns)
        {
            this.writer.WriteStartAttribute(prefix, localName, ns);
        }

        public override void WriteFullEndElement()
        {
            this.writer.WriteFullEndElement();
        }

        public override void WriteStartElement(string prefix, string localName, string ns)
        {
            this.writer.WriteStartElement(prefix, localName, ns);
        }

        public override void WriteEndElement()
        {
            this.writer.WriteEndElement();
        }

        public override void WriteDocType(string name, string pubid, string sysid, string subset)
        {
            this.writer.WriteDocType(name, pubid, sysid, subset);
        }

        public override void WriteEndDocument()
        {
            this.writer.WriteEndDocument();
        }

        public override void WriteStartDocument(bool standalone)
        {
            this.writer.WriteStartDocument(standalone);
        }

        public override void WriteStartDocument()
        {
            this.writer.WriteStartDocument();
        }
    }
}

这篇关于不间断空格的序列化选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆