简体   繁体   English

DataContractSerializer没有正确反序列化,缺少对象中方法的值

[英]DataContractSerializer does not properly deserialize, values for methods in object are missing

My SomeClass 我的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文件:

<?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. 因此,当我检查“someClass”时,FirstName将具有值john,但LastName将为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. 因此,反序列化时,DataContractSerializer不会从xml中提取所有数据。

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 如果有人遇到同样的问题,或者任何人有解决方案,请告诉我

Well i found my own answering after playing it a lot around....it has to be in an alpahbetical order. 好吧,我在玩了很多之后找到了自己的回答......它必须是一个alpahbetical顺序。 so if class has 所以如果上课了

[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; }
    }
}

then xml should be define alphabetically. 那么xml应该按字母顺序定义。

<?xml version="1.0" encoding="UTF-8"?>
 <SomeClass>
 <CustomValues>
    <Value1>One</Value1>
    <Value2>Two</Value2>
 </CustomValues >
 <FirstName>John</FirstName>
 <LastName>Smith</LastName>
 </SomeClass>

well why the h... it has to be in alphabetical? 为什么h ...它必须按字母顺序排列?

That's what the Order attribute is for; 这就是Order属性的用途; if you don't have control on the XML you deserialize, that's the only workaround I know about. 如果你无法控制你反序列化的XML,那就是我所知道的唯一解决方法。

In your first example that would be 在你的第一个例子中

[Serializable]
[DataContract(Namespace = "")]
public class SomeClass
{
    [DataMember(Order = 0)]
    public string FirstName
    { 
        get; set;
    }

    [DataMember(Order = 1)]
    public string LastName
    { 
        get; set;
    }

    [DataMember(Order = 2)]
    private IDictionary customValues;
    public IDictionary CustomValues
    {
        get { return customValues; }
        set { customValues = value; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM