如何重写XmlTextReader字符实体扩展?

发布于 2025-01-08 08:10:23 字数 856 浏览 0 评论 0原文

使用 .Net 框架,我需要将 XML 实体文本读取为原始字符串值(未扩展的字符实体),以便在比较/合并函数中使用。据我所知,没有办法直接关闭角色实体扩展。

我尝试从 XmlTextReader 派生并挂钩 Read() 方法,该方法会拦截读取,但 Value 属性是只读的,我看不到任何修改传入文本的方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace blah {
    class XmlRawTextReader : XmlTextReader {
        public XmlRawTextReader(string fileName) : base(fileName) { }

        public override bool Read() {
            bool result = base.Read();
            if (result == true && base.HasValue && base.NodeType == XmlNodeType.Text) {
                string s = this.Value;
                //this.Value = @"new value";  // does not work - read-only
            }
            return result;
        }
    }
}

有谁知道如何禁用字符实体扩展或在读取字符串时更新字符串?

有点卡在这里,所以提前感谢您的想法......

Using the .Net framework, I need to read XML entity text as raw string values (character entities not expanded) for use in a comparison / merge function. As far as I can tell, there's no way of directly turning off character entity expansion.

I've tried deriving from XmlTextReader and hooking the Read() method, which does intercept reads, but the Value property is read-only and I can't see any way of modifying the incoming text:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace blah {
    class XmlRawTextReader : XmlTextReader {
        public XmlRawTextReader(string fileName) : base(fileName) { }

        public override bool Read() {
            bool result = base.Read();
            if (result == true && base.HasValue && base.NodeType == XmlNodeType.Text) {
                string s = this.Value;
                //this.Value = @"new value";  // does not work - read-only
            }
            return result;
        }
    }
}

Does anyone know how to either disable character entity expansion or updating strings at the point they're read?

Kinda stuck here so thanks in advance for ideas...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

默嘫て 2025-01-15 08:10:23

将其放在次要位置一段时间后,答案变得显而易见:

虽然您不能轻松地重写 Read() 方法来更改读取值,但您可以挂钩属性访问器来执行相同的操作

using System;
using System.Collections.Generic;
using System.Xml;

namespace blah {

    class XmlCustomTextReader : XmlTextReader {

        private Func<string, List<KeyValuePair<string, string>>, string> _updateFunc;

        public XmlCustomTextReader(string fileName, Func<string, List<KeyValuePair<string, string>>, string> updateFunc = null) : base(fileName) {
            _updateFunc = updateFunc;
        }

        //
        // intercept and override value access - 'un-mangle' strings that were rewritten by XMLTextReader
        public override string Value {
            get {
                string currentvalue = base.Value;

                // only text nodes
                if (NodeType != XmlNodeType.Text)
                    return currentvalue;

                string newValue = currentvalue;

                // if a conversion function was provided, use it to update the string
                if (_updateFunc != null)
                    newValue = _updateFunc(currentvalue, null);

                return newValue;
            }
        }
    }
}

: :

        Func<string, List<KeyValuePair<string, string>>, string> updateFunc = UpdateString;
        XmlCustomTextReader reader = new XmlCustomTextReader(fileName, updateFunc);
        reader.XmlResolver = new XmlCustomResolver(XmlCustomResolver.ResolverType.useResource);
        XDocument targetDoc = XDocument.Load(reader);

我希望这对将来的人有帮助......

After putting this on the back-burner for a while, the answer became apparent:

Although you can't easily override the Read() method to change the read value, you can hook the property accessor to do the same thing:

using System;
using System.Collections.Generic;
using System.Xml;

namespace blah {

    class XmlCustomTextReader : XmlTextReader {

        private Func<string, List<KeyValuePair<string, string>>, string> _updateFunc;

        public XmlCustomTextReader(string fileName, Func<string, List<KeyValuePair<string, string>>, string> updateFunc = null) : base(fileName) {
            _updateFunc = updateFunc;
        }

        //
        // intercept and override value access - 'un-mangle' strings that were rewritten by XMLTextReader
        public override string Value {
            get {
                string currentvalue = base.Value;

                // only text nodes
                if (NodeType != XmlNodeType.Text)
                    return currentvalue;

                string newValue = currentvalue;

                // if a conversion function was provided, use it to update the string
                if (_updateFunc != null)
                    newValue = _updateFunc(currentvalue, null);

                return newValue;
            }
        }
    }
}

Use this by:

        Func<string, List<KeyValuePair<string, string>>, string> updateFunc = UpdateString;
        XmlCustomTextReader reader = new XmlCustomTextReader(fileName, updateFunc);
        reader.XmlResolver = new XmlCustomResolver(XmlCustomResolver.ResolverType.useResource);
        XDocument targetDoc = XDocument.Load(reader);

I hope this helps someone in the future...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文