我有一个KeyValuePair对象XML序列化,并为每一个关键的C#KeyValuePair反序列化,有像这样的值的列表:列表作为“值”字段
<WritableKeyValuePairOfKeyContraenteArrayOfAddress>
<key>
<KeyContaente>123456</KeyContaente>
</key>
<value>
<Address>Maple road 12</Address>
</value>
<value>
<Address>Oak street 71</Address>
</value>
</WritableKeyValuePairOfKeyContraenteArrayOfAddress>
的问题是:当我反序列化,对象的值只包含一个元素:第一个(“枫树道路12”)。为什么会发生?
编辑: 序列化:
public static string Serializza<T>(T objectToSerialize)
{
using (MemoryStream memStm = new MemoryStream())
{
var serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(memStm, objectToSerialize);
memStm.Seek(0, SeekOrigin.Begin);
using (var streamReader = new StreamReader(memStm))
{
string result = streamReader.ReadToEnd();
return result;
}
}
}
反序列化:
private static T Deserializza<T>(string xmlString)
{
if (xmlString.Length > 0)
{
try
{
var serializer = new DataContractSerializer(typeof(T));
using (System.IO.MemoryStream memStm = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xmlString)))
{
return (T)serializer.ReadObject(memStm);
}
}
catch
{
return default(T);
}
}
else
return default(T);
}
KeyValuePair就像是一个字典:
[Serializable, StructLayout(LayoutKind.Sequential)]
public class WritableKeyValuePair<TKey, TValue> : IWritableKeyValuePair
{
private TKey key;
private TValue value;
public WritableKeyValuePair() { }
public WritableKeyValuePair(TKey key, TValue value)
{
this.key = key;
this.value = value;
}
public override string ToString()
{
return WKVPUtils<TKey, TValue>.ToString(key, value);
}
////[XmlProcessDeep()]
public TValue Value
{
get { return this.value; }
set { this.value = value; }
}
public TKey Key
{
get { return this.key; }
set { this.key = value; }
}
internal static class WKVPUtils<TKey, TValue>
{
public static string ToString(TKey Key, TValue Value)
{
StringBuilder builder1 = new StringBuilder();
builder1.Append('[');
if (Key != null)
builder1.Append(Key.ToString());
builder1.Append(", ");
if (Value != null)
builder1.Append(Value.ToString());
builder1.Append(']');
return builder1.ToString();
}
}
}
向我们显示代码。具体如何序列化?到底你如何反序列化?你在说什么KeyValuePair? –
参见op – red9350
中的修改信息:'visual-studio'标签上显示:“除非您有关于Visual Studio的具体问题 - 不只是编码问题,否则不要使用此标签”。我已经移除了标签。 – Amy