DataContractSerializer 未正确反序列化,对象中方法的值丢失
我的 SomeClass
[Serializable]
[DataContract(Namespace = "")]
public class SomeClass
{
[DataMember]
public string FirstName
{
get; set;
}
[DataMember]
public string LastName
{
get; set;
}
[DataMember]
private IDictionary<long, string> customValues;
public IDictionary<long, string> CustomValues
{
get { return customValues; }
set { customValues = value; }
}
}
我的 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<SomeClass>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<CustomValues>
<Value1>One</Value1>
<Value2>Two</Value2>
</CustomValues >
</SomeClass>
但我的问题是类,当我反序列化时,我只获取我的方法的一些数据。
var xmlRoot = XElement.Load(new StreamReader(
filterContext.HttpContext.Request.InputStream,
filterContext.HttpContext.Request.ContentEncoding));
XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(xmlRoot.CreateReader());
DataContractSerializer ser = new DataContractSerializer(typeof(SomeClass));
//Deserialize the data and read it from the instance.
SomeClass someClass = (SomeClass)ser.ReadObject(reader, true);
因此,当我检查“someClass”时,FirstName 的值为 john,但 LastName 将为 null。
神秘的是我如何才能获得该类的一些数据而不是所有数据。 因此,DataContractSerializer 在反序列化时不会从 xml 中提取所有数据。
我做错了什么吗?
任何帮助表示赞赏。提前致谢。
让我知道是否有人遇到同样的问题或有人有解决方案
My SomeClass
[Serializable]
[DataContract(Namespace = "")]
public class SomeClass
{
[DataMember]
public string FirstName
{
get; set;
}
[DataMember]
public string LastName
{
get; set;
}
[DataMember]
private IDictionary<long, string> customValues;
public IDictionary<long, string> CustomValues
{
get { return customValues; }
set { customValues = value; }
}
}
My XML File:
<?xml version="1.0" encoding="UTF-8"?>
<SomeClass>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<CustomValues>
<Value1>One</Value1>
<Value2>Two</Value2>
</CustomValues >
</SomeClass>
But my problem is for the class, i am only getting some of the data for my methods when i deserialize.
var xmlRoot = XElement.Load(new StreamReader(
filterContext.HttpContext.Request.InputStream,
filterContext.HttpContext.Request.ContentEncoding));
XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(xmlRoot.CreateReader());
DataContractSerializer ser = new DataContractSerializer(typeof(SomeClass));
//Deserialize the data and read it from the instance.
SomeClass someClass = (SomeClass)ser.ReadObject(reader, true);
So when I check "someClass", FirstName will have the value john, But the LastName will be null.
Mystery is how can i get some of the data and not all of the data for the class.
So DataContractSerializer is not pulling up all the data from xml when deserializing.
Am i doing something wrong.
Any help is appreciated. Thanks in advance.
Let me know if anyone has the same problem or any one has solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,在玩了很多次之后,我找到了自己的答案......它必须按字母顺序排列。
所以如果类有
那么 xml 应该按字母顺序定义。
那么为什么...它必须按字母顺序排列?
Well i found my own answering after playing it a lot around....it has to be in an alpahbetical order.
so if class has
then xml should be define alphabetically.
well why the h... it has to be in alphabetical?
这就是
Order
属性的用途;如果您无法控制反序列化的 XML,这是我所知道的唯一解决方法。在你的第一个例子中,那就是
That’s what the
Order
attribute is for; if you don’t have control on the XML you deserialize, that’s the only workaround I know about.In your first example that would be