C#使用多个XML声明反序列化XML

时间:2012-07-09 11:02:53

标签: c# xml xml-deserialization

第三方供应商为我们提供了格式不正确的XML。它有多个XML声明:

<?xml version="1.0" encoding="utf-16"?><!DOCTYPE bob />
<?xml version="1.0"?><!DOCTYPE jim>

<elements>
...
</elements

我的反序列化代码:

 var serializer = new XmlSerializer(response.GetType());

                reader = new XmlTextReader(stream) {XmlResolver = null};

                result = (IResponse) serializer.Deserialize(reader);

我遇到的问题是反序列化程序抱怨多个XML声明。有没有办法可以删除声明,以便XML成功反序列化?

由于

1 个答案:

答案 0 :(得分:1)

您可以在XmlReader周围编写一个包装器,它会过滤掉后续的xml处理指令和文档类型。

public class XmlFilteringReader : XmlReader
{
    private readonly XmlReader _source;
    private bool _gotXmlDeclaration = false;
    private bool _gotDoctype = false;

    public XmlFilteringReader(XmlReader source)
    {
        _source = source;
    }

    public override bool Read()
    {
        var ok = _source.Read();
        if (ok && _source.NodeType == XmlNodeType.ProcessingInstruction
                && _source.LocalName == "xml")
        {
            if (_gotXmlDeclaration) return Read(); // Recursive
            _gotXmlDeclaration = true;
        }
        else if (ok && _source.NodeType == XmlNodeType.DocumentType)
        {
            if (_gotDoctype) return Read(); // Recursive
            _gotDoctype = true;
        }
        return ok;
    }

    // Implementation of other methods and properties
    // by calling the same method or property on _source
}
var serializer = new XmlSerializer(response.GetType());
var reader = new XmlFilteringReader(new XmlTextReader(stream) {XmlResolver = null});
var result = (IResponse) serializer.Deserialize(reader);

使用XmlWrappingReader库中的Mvp.Xml可以简化实施过程。关于此,还有a blog-post