DataContractSerializer 未正确反序列化,对象中方法的值丢失

发布于 2024-08-09 16:23:13 字数 1584 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-08-16 16:23:13

好吧,在玩了很多次之后,我找到了自己的答案......它必须按字母顺序排列。
所以如果类有

[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>
 <CustomValues>
    <Value1>One</Value1>
    <Value2>Two</Value2>
 </CustomValues >
 <FirstName>John</FirstName>
 <LastName>Smith</LastName>
 </SomeClass>

那么为什么...它必须按字母顺序排列?

Well i found my own answering after playing it a lot around....it has to be in an alpahbetical order.
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 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?

秋叶绚丽 2024-08-16 16:23:13

这就是 Order 属性的用途;如果您无法控制反序列化的 XML,这是我所知道的唯一解决方法。

在你的第一个例子中,那就是

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

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

[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; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文