序言:注意添加“新”
Add
这并不能阻止人们给老人打电话
添加
仅仅通过铸造。此外,就“墨西哥商品交易所”而言,这是一个非常模糊的数据合同——它需要如此开放吗?(很多
object
等等)
第一:你错过了几个吗
[DataContract]
/
[DataMember]
标记在那里?特别地:
你能澄清一下吗
确切地
哪个版本的。NET你在用什么?
DataContractSerializer
其他功能已通过服务包进行了调整。安装了3.5 SP1(这是我唯一能拿到的东西)后,它至少可以通过以下方式进行序列化和反序列化
DataContractSerializer
(无WCF堆栈),以及正确的
添加
方法被调用。
你能检查一下以下内容是否适用于你的本地版本吗?(它适用于3.5 SP1和缺少的属性)[先输出]:
1
MyDictionary
abc=123
def=ghi
代码:
// or long-hand in C# 2.0
ParameterClass pc = new ParameterClass {
Data = new List<ParmData> { new ParmData {
Value = new MyDictionary {
{"abc",123},
{"def","ghi"}
}}}};
DataContractSerializer dcs = new DataContractSerializer(pc.GetType());
string xml;
using(StringWriter sw = new StringWriter())
using(XmlWriter xw = XmlWriter.Create(sw)) {
dcs.WriteObject(xw, pc);
xw.Close();
xml = sw.ToString();
}
using(StringReader sr = new StringReader(xml)) {
ParameterClass clone = (ParameterClass)dcs.ReadObject(XmlReader.Create(sr));
Console.WriteLine(clone.Data.Count);
Console.WriteLine(clone.Data[0].Value.GetType().Name);
MyDictionary d = (MyDictionary)clone.Data[0].Value;
foreach (KeyValuePair<string, object> pair in d)
{
Console.WriteLine("{0}={1}", pair.Key, pair.Value);
}
}
显然,这只是测试
DataContractSerializer
(没有整个WCF堆栈),但它似乎可以工作……所以:相同的代码是否适用于您的本地版本。网?如果没有,是否可以选择安装最新的3.0 service pack?(最好是通过安装3.5 SP1)。
为了获取更多信息,我获取了xml:
<?xml version="1.0" encoding="utf-16"?><ParameterClass xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/"><Data><ParmData><Value xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:type="d4p1:ArrayOfKeyValueOfstringanyType"><d4p1:KeyValueOfstringanyType><d4p1:Key>abc</d4p1:Key><d4p1:Value xmlns:d6p1="http://www.w3.org/2001/XMLSchema" i:type="d6p1:int">123</d4p1:Value></d4p1:KeyValueOfstringanyType><d4p1:KeyValueOfstringanyType><d4p1:Key>def</d4p1:Key><d4p1:Value xmlns:d6p1="http://www.w3.org/2001/XMLSchema" i:type="d6p1:string">ghi</d4p1:Value></d4p1:KeyValueOfstringanyType></Value></ParmData></Data></ParameterClass>