XmlSerializer的改变.NET 3.5 SP1 [英] XmlSerializer changes in .NET 3.5 SP1

查看:135
本文介绍了XmlSerializer的改变.NET 3.5 SP1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了变化,.NET 3.5 SP1相当多的职位,但偶然到一个我还没有看到昨天的文档。我有code工作就好了我的机器上,从VS,MSBuild的命令行,一切,但它未能在构建服务器(运行.NET 3.5 RTM)。

I've seen quite a few posts on changes in .NET 3.5 SP1, but stumbled into one that I've yet to see documentation for yesterday. I had code working just fine on my machine, from VS, msbuild command line, everything, but it failed on the build server (running .NET 3.5 RTM).

[XmlRoot("foo")]
public class Foo
{
    static void Main()
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Foo));

        string xml = @"<foo name='ack' />";
        using (StringReader sr = new StringReader(xml))
        {
            Foo foo = serializer.Deserialize(sr) as Foo;
        }
    }

    [XmlAttribute("name")]
    public string Name { get; set; }

    public Foo Bar { get; private set; }
}

在SP1中,上述code运行得很好。在RTM,你会得到一个InvalidOperationException:

In SP1, the above code runs just fine. In RTM, you get an InvalidOperationException:

无法生成临时类(结果= 1)。   错误CS0200:属性或索引ConsoleApplication2.Foo.Bar'不能被分配​​到 - 它是只读

Unable to generate a temporary class (result=1). error CS0200: Property or indexer 'ConsoleApplication2.Foo.Bar' cannot be assign to -- it is read only

当然,所有的需要,使其RTM下运行是添加[XmlIgnore]的酒吧财产。

Of course, all that's needed to make it run under RTM is adding [XmlIgnore] to the Bar property.

我的谷歌赋显然没有达到找到这种变化的文档。是否有一个变化列表的任意位置,列出这一变化(以及类似的可能跳起来喊疑难杂症在引擎罩的变化)?这是一个错误或功能?

My google fu is apparently not up to finding documentation of these kinds of changes. Is there a change list anywhere that lists this change (and similar under-the-hood changes that might jump up and shout "gotcha")? Is this a bug or a feature?

修改:在SP1中,如果我添加了一个&LT;酒吧/&GT; 元素,或将[的XmlElement]为律师的财产,它不会反序列化。当它试图反序列化不会失败pre-SP1 - 它抛出时,XmlSerializer的构造异常

EDIT: In SP1, if I added a <Bar /> element, or set [XmlElement] for the Bar property, it won't get deserialized. It doesn't fail pre-SP1 when it tries to deserialize--it throws an exception when the XmlSerializer is constructed.

这让我瘦更倾向于这是一个错误,特别是如果我设置Foo.Bar的[的XmlElement]属性。如果它不能做什么,我要求它做的事,应该是抛出一个异常,而不是默默忽略Foo.Bar。其他无效组合/的XML序列化的属性设置,导致异常。

This makes me lean more toward it being a bug, especially if I set an [XmlElement] attribute for Foo.Bar. If it's unable to do what I ask it to do, it should be throwing an exception instead of silently ignoring Foo.Bar. Other invalid combinations/settings of XML serialization attributes result in an exception.

修改:谢谢你, TonyB ,我不知道如何设置的临时文件的位置。对于那些遇到类似的问题,在未来,你就需要一个额外的配置标志:

EDIT: Thank you, TonyB, I'd not known about setting the temp files location. For those that come across similar issues in the future, you do need an additional config flag:

<system.diagnostics>
  <switches>
    <add name="XmlSerialization.Compilation" value="1" />
  </switches>
</system.diagnostics>
<system.xml.serialization>
  <xmlSerializer tempFilesLocation="c:\\foo"/>
</system.xml.serialization>

其中相当坚定地把这个在无声地吞噬错误(又名错误)的境界 -

即使使用设置在酒吧财产[的XmlElement]属性,没有提及是在生成的序列组装用它制成的。如果不是这样,设计师们决定[XmlIgnore]不再必要的属性,不能设置 - 你希望看到,在发行说明,的更改列表的,或者的 XmlIgnoreAttribute文档

Even with setting an [XmlElement] attribute on the Bar property, no mention was made of it in the generated serialization assembly--which fairly firmly puts this in the realm of a silently swallowed error (aka, a bug). Either that or the designers have decided [XmlIgnore] is no longer necessary for properties that can't be set--and you'd expect to see that in release notes, change lists, or the XmlIgnoreAttribute documentation.

推荐答案

在SP1并在foo.Bar财产得到适当的反序列化?

In SP1 does the foo.Bar property get properly deserialized?

在pre SP1,你将无法反序列化的对象,因为律师属性设置方法是私有所以XmlSerializer的没有一种方法来设置该值。我不知道如何SP1是拉它关闭。

In pre SP1 you wouldn't be able to deserialize the object because the set method of the Bar property is private so the XmlSerializer doesn't have a way to set that value. I'm not sure how SP1 is pulling it off.

您可以尝试添加到您的web.config / app.config中

You could try adding this to your web.config/app.config

<system.xml.serialization> 
  <xmlSerializer tempFilesLocation="c:\\foo"/> 
</system.xml.serialization>

这将使由XmlSerializer的生成的类到C:\ FOO所以你可以看到它是什么做的SP1 RTM VS

That will put the class generated by the XmlSerializer into c:\foo so you can see what it is doing in SP1 vs RTM

这篇关于XmlSerializer的改变.NET 3.5 SP1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆