使用CollectionDataContract时,DataContractSerializer为所有值返回null

时间:2018-01-25 17:20:12

标签: c# serialization datacontractserializer

我在这里引用了一些数据http://docs.microsofttranslator.com/text-translate.html#!/default/post_TranslateArray

<ArrayOfTranslateArrayResponse 
    xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <TranslateArrayResponse>
        <From>en</From>
        <OriginalTextSentenceLengths 
            xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <a:int>14</a:int>
        </OriginalTextSentenceLengths>
        <TranslatedText>Esto es una prueba</TranslatedText>
        <TranslatedTextSentenceLengths 
            xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <a:int>18</a:int>
        </TranslatedTextSentenceLengths>
    </TranslateArrayResponse>
    <TranslateArrayResponse>
        <From>en</From>
        <OriginalTextSentenceLengths 
            xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <a:int>22</a:int>
        </OriginalTextSentenceLengths>
        <TranslatedText>Hola, yo tengo una manzana</TranslatedText>
        <TranslatedTextSentenceLengths 
            xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <a:int>26</a:int>
        </TranslatedTextSentenceLengths>
    </TranslateArrayResponse>
</ArrayOfTranslateArrayResponse>

我正在尝试使用DataContractSerializer反序列化此数据。集合中的所有值都为null。我可以使用相同的结构序列化,它与此文本相同,但它不会反序列化。这是代码。

void Main()
{
    var xmlString = "<ArrayOfTranslateArrayResponse xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><TranslateArrayResponse><From>language-code</From><OriginalTextSentenceLengths xmlns:a=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"><a:int>int-value</a:int></OriginalTextSentenceLengths><State/><TranslatedText>string-value</TranslatedText><TranslatedTextSentenceLengths xmlns:a=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"><a:int>int-value</a:int></TranslatedTextSentenceLengths></TranslateArrayResponse></ArrayOfTranslateArrayResponse>";
    var dcs = new DataContractSerializer(typeof(TranslateArrayResponseCollection));

    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    {
        writer.Write(xmlString);
        writer.Flush();
        stream.Position = 0;

        dcs.ReadObject(stream).Dump();
    }
}

[CollectionDataContract(Name = "ArrayOfTranslateArrayResponse", Namespace = "http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2")]
public class TranslateArrayResponseCollection : List<TranslateArrayResponse> { }

[DataContract(Name = "TranslateArrayResponse", Namespace = "")]
public class TranslateArrayResponse
{
    [DataMember]
    public string Error { get; set; }

    [DataMember]
    public string From { get; set; }

    [DataMember]
    public int[] OriginalSentenceLengths { get; set; }

    [DataMember]
    public string TranslatedText { get; set; }

    [DataMember]
    public int[] TranslatedSentenceLengths { get; set; }

    [DataMember]
    public string State { get; set; }
}

1 个答案:

答案 0 :(得分:2)

在XML数据中,数组的名称为OriginalTextSentenceLengthsTranslatedTextSentenceLengths。但在您的数据合同中,它们是OriginalSentenceLengthsTranslatedSentenceLengths。您应该在数据合同中重命名它们:

[DataMember]
public string TranslatedText { get; set; }

[DataMember]
public int[] TranslatedTextSentenceLengths { get; set; }

另一个问题是缺少元素的命名空间。在我向所有元素添加空名称空间xmlns=""后,数据被正确反序列化。这是修改后的XML:

<ArrayOfTranslateArrayResponse 
    xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <TranslateArrayResponse>
        <From xmlns="">en</From>
        <OriginalTextSentenceLengths xmlns=""
            xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <a:int>14</a:int>
        </OriginalTextSentenceLengths>
        <TranslatedText xmlns="">Esto es una prueba</TranslatedText>
        <TranslatedTextSentenceLengths xmlns=""
            xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <a:int>18</a:int>
        </TranslatedTextSentenceLengths>
    </TranslateArrayResponse>
    <TranslateArrayResponse>
        <From xmlns="">en</From>
        <OriginalTextSentenceLengths xmlns=""
            xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <a:int>22</a:int>
        </OriginalTextSentenceLengths>
        <TranslatedText xmlns="">Hola, yo tengo una manzana</TranslatedText>
        <TranslatedTextSentenceLengths xmlns=""
            xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <a:int>26</a:int>
        </TranslatedTextSentenceLengths>
    </TranslateArrayResponse>
</ArrayOfTranslateArrayResponse>

我认为这是DataContractSerializer的限制。检查此question是否存在完全相同的问题。您应该通过添加命名空间来修改源XML,或者使用XmlSerializer来处理这种情况。