4

我正在与向我们发送 XML 消息的第三方应用程序集成。他们的 XML 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE theirObj SYSTEM "theirDTD-2.0.dtd">
<theirObj>
    <properties>
        <datasource>ThirdParty</datasource>
        <datetime>2009-03-05T14:45:39</datetime>
    </properties>
        <data>
          ...
        </data>
</theirObj>

我正在尝试使用 XmlSerializer 反序列化它:

public theirObj Deserialize(string message) {
            if( string.IsNullOrWhiteSpace( message ) ) {
                throw new ArgumentNullException( "message" );
            }
            XmlSerializer xmlSerializer = new XmlSerializer( typeof(theirObj ) );

            TextReader textReader = new StringReader( message );

            using (XmlReader xmlReader = new XmlTextReader( textReader )) {
                object deserializedObject = xmlSerializer.Deserialize( xmlReader );

                theirObj ent = deserializedObject as theirObj ;

                if (ent == null) {
                    throw new InvalidCastException("Unable to cast deserialized object to an theirObj object. {0}".FormatInvariant( deserializedObject));
                }

                return ent;
            }
        }
}

我使用 xsd.exe 生成了对象。

如果我删除<!DOCTYPE>标签,那么它反序列化很好。

有没有办法让 XmlSerializer 忽略<!DOCTYPE>标签?

我知道我可以在将它传递给 XmlSerializer 之前将其删除,但如果我不需要,我宁愿不去那种级别的 XML 操作。

4

3 回答 3

6

而不是使用XmlTextReader,调用XmlReader.Create并传递一个设置为的XmlReaderSettings对象:DtdProcessingIgnore

TextReader textReader = new StringReader( message );
var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };

using (XmlReader xmlReader = XmlReader.Create(textReader, settings))

注意:DtdProcessing属性是在 .NET 4.0 中添加的。在 .NET 3.5 中,您可以改为设置ProhibitDtdfalse和:XmlResolvernull

var settings = new XmlReaderSettings { ProhibitDtd = false, XmlResolver = null };
于 2013-01-21T22:21:38.950 回答
0

DOCTYPE 没有内置的 XmlSerlization 属性。事实上,这是因为 XML 序列化是基于元素而不是基于文档的。我认为您可以使用以下方法在序列化中跳过 DOCTYPE:

public static String Serialize(object obj)
{
    StringBuilder builder = new StringBuilder();
    XmlSerializer serializer = new XmlSerializer(typeof(theirObj));

    using (XmlWriter writer = XmlWriter.Create(builder, new XmlWriterSettings() { OmitXmlDeclaration = true }))
        xmlSerializer.Serialize(writer, obj);

    return builder.ToString();
}

然后,在文档反序列化后将其注入回去。

于 2013-01-21T22:06:10.900 回答
0

您可以删除文档类型

TextReader textReader = new StringReader( message );
XmlDocument XDoc = new XmlDocument();
XDoc.Load(textReader);
XmlDocumentType XDType = XDoc.DocumentType;
XDoc.RemoveChild(XDType);

using (XmlReader xmlReader = new XmlTextReader(XDoc)) {
                object deserializedObject = xmlSerializer.Deserialize( xmlReader );

                theirObj ent = deserializedObject as theirObj ;

                if (ent == null) {
                    throw new InvalidCastException("Unable to cast deserialized object to an theirObj object. {0}".FormatInvariant( deserializedObject));
                }

                return ent;
            }
于 2013-01-21T22:11:45.790 回答