如何重写XmlTextReader字符实体扩展?
使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其放在次要位置一段时间后,答案变得显而易见:
虽然您不能轻松地重写 Read() 方法来更改读取值,但您可以挂钩属性访问器来执行相同的操作
: :
我希望这对将来的人有帮助......
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:
Use this by:
I hope this helps someone in the future...