使用 CollectionDataContract 时,DataContractSerializer 为所有值返回 null
DataContractSerializer returning null for all values when using CollectionDataContract
我这里引用了一些数据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
反序列化此数据。集合中的所有值都为空。我可以使用相同的结构进行序列化,结果与本文相同,但不会反序列化。这是代码。
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; }
}
在您的 XML 中,数据数组被命名为 OriginalTextSentenceLengths
和 TranslatedTextSentenceLengths
。但在您的数据合同中,它们是 OriginalSentenceLengths
和 TranslatedSentenceLengths
。您应该在数据合同中重命名它们:
[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
。
我这里引用了一些数据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
反序列化此数据。集合中的所有值都为空。我可以使用相同的结构进行序列化,结果与本文相同,但不会反序列化。这是代码。
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; }
}
在您的 XML 中,数据数组被命名为 OriginalTextSentenceLengths
和 TranslatedTextSentenceLengths
。但在您的数据合同中,它们是 OriginalSentenceLengths
和 TranslatedSentenceLengths
。您应该在数据合同中重命名它们:
[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
。