本文介绍了如何创建一个忽略命名空间且不检查字符的XmlTextReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 XmlTextReader 来忽略命名空间并且不检查字符.要忽略名称空间,我可以设置属性 Namespaces = false ,并且不检查字符,可以使用 XmlReaderSettings.CheckCharacters = false .

我试图使用它的构造函数创建 XmlTextReader ,但这不允许我传入已配置的 XmlReaderSettings .属性 Settings 是只读的,因此构造后无法对其进行设置.

  XmlTextReader reader =新的XmlTextReader(gzs){命名空间= false};reader.Settings = new XmlReaderSettings {CheckCharacters = false};//只读 

使用基类 XmlReader 的静态方法 Create(),我可以传入 XmlReaderSettings ,但是该方法返回一个 XmlTextReaderImpl ,并且没有要设置的属性 Namespace ,因此无法将其强制转换为 XmlTextReader .

  var settings = new XmlReaderSettings {CheckCharacters = false};var reader = XmlTextReader.Create(gzs,settings);XmlTextReader textReader =作为XmlTextReader的阅读器//不可能 

那么如何创建这样的XmlTextReader?可能吗? XmlReaderSettings 上是否有任何属性可以忽略名称空间?

解决方案

这并不难.

我的任务是反序列化具有或没有默认名称空间声明的相同结构的xml文件.

首先,我发现了讨论,XmlTextReader解决方案:NamespaceIgnorantXmlTextReader.但是MSDN建议不要使用XmlTextReader,而应使用XmlReader.

所以我们需要在XmlReader上做同样的事情.

XmlReader是一个抽象类,我们需要先做一个包装器.此处是一篇很好的文章.并且此处是现成的XmlWrappingReader.>

然后我们需要扩展它:

 公共类XmlExtendableReader:XmlWrappingReader{私人布尔_ignoreNamespace {get;放;}公共XmlExtendableReader(TextReader输入,XmlReaderSettings设置,bool ignoreNamespace = false):基础(XmlReader.Create(输入,设置)){_ignoreNamespace = ignoreNamespace;}公共重写字符串NamespaceURI{得到{返回_ignoreNamespace吗?String.Empty:base.NamespaceURI;}}} 

就是这样.现在,我们可以使用它并控制XmlReaderSettings和名称空间处理:

  XmlReaderSettings设置=新的XmlReaderSettings(){CheckCharacters = false,ConformanceLevel = ConformanceLevel.Document,DtdProcessing = DtdProcessing.Ignore,IgnoreComments = true,IgnoreProcessingInstructions = true,IgnoreWhitespace = true,ValidationType = ValidationType.None};使用(XmlExtendableReader xmlreader = new XmlExtendableReader(reader,settings,true))_entities =(((订单)_serializer.Deserialize(xmlreader)).Order; 

I want to use a XmlTextReader that ignores Namespaces and does not check characters. To ignore namespaces I can set the property Namespaces=false and to not check characters I can use the XmlReaderSettings.CheckCharacters = false.

I tried to create the XmlTextReader using it constructors, but that does not allow me to pass in a configured XmlReaderSettings. The property Settings is readonly so that I cannot set it after construction.

XmlTextReader reader = new XmlTextReader(gzs) { Namespaces = false};
reader.Settings = new XmlReaderSettings { CheckCharacters = false}; // readonly

Using the static method Create() of the base class XmlReader I can pass in the XmlReaderSettings, but the method returns a XmlTextReaderImpl and that has no property Namespace to set and it cannot be cast to XmlTextReader.

var settings = new XmlReaderSettings { CheckCharacters = false}; 
var reader = XmlTextReader.Create(gzs, settings); 
XmlTextReader textReader = reader as XmlTextReader  // not possible

So how do I create such a XmlTextReader? Is it even possible? Are there any properties on XmlReaderSettings to ignore namespaces?

解决方案

It is not that hard.

My task was to deserialize xml files of the same structure which can be with or without a default namespace declaration.

First I found this discussion with the XmlTextReader solution: NamespaceIgnorantXmlTextReader.But MSDN recommends not to use XmlTextReader and use XmlReader instead.

So we need to do the same thing on the XmlReader.

XmlReader is an abstract class and we need to do a wrapper first. Here is a good article how to do it. And here is a ready XmlWrappingReader.

Then we need to extend it:

public class XmlExtendableReader : XmlWrappingReader
{
    private bool _ignoreNamespace { get; set; }

    public XmlExtendableReader(TextReader input, XmlReaderSettings settings, bool ignoreNamespace = false)
    : base(XmlReader.Create(input, settings))
    {
        _ignoreNamespace = ignoreNamespace;
    }

    public override string NamespaceURI
    {
        get
        {
            return _ignoreNamespace ? String.Empty : base.NamespaceURI;
        }
    }
}

That's it. Now we can use it and control both XmlReaderSettings and a namespace treatment:

XmlReaderSettings settings = new XmlReaderSettings()
{
    CheckCharacters = false,
    ConformanceLevel = ConformanceLevel.Document,
    DtdProcessing = DtdProcessing.Ignore,
    IgnoreComments = true,
    IgnoreProcessingInstructions = true,
    IgnoreWhitespace = true,
    ValidationType = ValidationType.None
};

using (XmlExtendableReader xmlreader = new XmlExtendableReader(reader, settings, true))
    _entities = ((Orders)_serializer.Deserialize(xmlreader)).Order;

这篇关于如何创建一个忽略命名空间且不检查字符的XmlTextReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:35