删除结束元素的空间?

编程入门 行业动态 更新时间:2024-10-27 01:34:10
本文介绍了删除结束元素的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目前我使用XML来存储多种数据和创建这些XML文件时,我想,以减少它的大小到最小,我可以。

Currently I am using XML to store many data and when creating those XML files I would like to reduce it is size to the minimum I can.

我怎么能覆盖的XmlWriter功能(对writeEndElement),因此而不是保存喜欢的:

How could I override the XmlWriter function (WriteEndElement) so instead of saving it like:

<thisElement someAttribute="blabla" />

它将被保存,如:

It will be saved like:

<thisElement someAttribute="blabla"/>

更新:

我试图找到一种方法使用到acomplish这样的:

I am trying to find a way to acomplish this by using:

public override void WriteEndElement()

但我不能在当前对writeEndElement功能知道我必须要改变它,如果它甚至有可能

But I can't the the current WriteEndElement function to know what I have to change on it and if it is even possible.

推荐答案

我恐怕是不可能无需重写代码的显著部分。空间是硬编码在内部类和不可配置。

I'm afraid it's not possible without rewriting significant portions of code. The space is hardcoded in the internal classes and not configurable.

例如,代码段内部 XmlEncodedRawTextWriter.WriteEndElement()方法。

e.g., Code snippet of the internal XmlEncodedRawTextWriter.WriteEndElement() method.

internal override void WriteEndElement(string prefix, string localName, string ns) { // snip... else { this.bufPos--; this.bufChars[this.bufPos++] = ' '; // the space is hard coded this.bufChars[this.bufPos++] = '/'; this.bufChars[this.bufPos++] = '>'; } }

这是你有我能想到的一些选项解析XML输出​​搜索结束标记手动删除的空间中,实现自己的XML作家所以它不包括此空间,或写一个包装类使用反射来修改内部缓冲区末尾元素被写入时

Some options that you have that I can think of are to parse the outputted XML to search for the closing tags to remove the space manually, implement your own XML writer so it doesn't include this space, or write a wrapper class to use reflection to modify the internal buffers when the end element is written.

下面是一个扩展方法,它可以做到这一点。一定要小心,这是不可移植。它也不是保证对所有案件的工作,虽然它似乎简单的情况下工作。我不认为什么都是在这里完成会破坏作家的状态,但是,是在您自担风险使用

Here's an extension method which could do that. Just be warned, this is not portable. Nor is it guaranteed to work for all cases though it seems to work for simple cases. I don't think what is done here would corrupt the state of the writer but, use at your own risk.

public static class XmlWriterExtensions { private static readonly Func<XmlWriter, object> get_writer; private static readonly Func<object, char[]> get_bufChars; private static readonly Func<object, int> get_bufPos; private static readonly Action<object, int> set_bufPos; static XmlWriterExtensions() { var asm = Assembly.GetAssembly(typeof(XmlWriter)); var xmlWellFormedWriterType = asm.GetType("System.Xml.XmlWellFormedWriter"); var flags = BindingFlags.NonPublic | BindingFlags.Instance; var writerField = xmlWellFormedWriterType.GetField("writer", flags); get_writer = w => writerField.GetValue(w); var xmlEncodedRawTextWriterType = asm.GetType("System.Xml.XmlEncodedRawTextWriter"); var bufCharsField = xmlEncodedRawTextWriterType.GetField("bufChars", flags); var bufPosField = xmlEncodedRawTextWriterType.GetField("bufPos", flags); get_bufChars = w => (char[])bufCharsField.GetValue(w); get_bufPos = w => (int)bufPosField.GetValue(w); set_bufPos = (w, i) => bufPosField.SetValue(w, i); } public static void TrimElementEnd(this XmlWriter writer) { var internalWriter = get_writer(writer); char[] bufChars = get_bufChars(internalWriter); int bufPos = get_bufPos(internalWriter); if (bufPos > 3 && bufChars[bufPos - 3] == ' ' && bufChars[bufPos - 2] == '/' && bufChars[bufPos - 1] == '>') { bufChars[bufPos - 3] = '/'; bufChars[bufPos - 2] = '>'; bufPos--; set_bufPos(internalWriter, bufPos); } } } // usage: Console.OutputEncoding = Encoding.UTF8; using (var writer = XmlWriter.Create(Console.Out)) { writer.WriteStartElement("Foo"); writer.WriteElementString("Bar", null); writer.TrimElementEnd(); writer.WriteElementString("Baz", null); writer.WriteEndElement(); }

<?xml version="1.0" encoding="utf-8"?><Foo><Bar/><Baz /></Foo>

更多推荐

删除结束元素的空间?

本文发布于:2023-11-30 11:10:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649763.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   结束   空间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!