Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

c# - How do I create a XmlTextReader that ignores Namespaces and does not check characters

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...