Note that there are some explanatory texts on larger screens.

plurals
  1. POXmlSerializer changes in .NET 3.5 SP1
    text
    copied!<p>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).</p> <pre><code>[XmlRoot("foo")] public class Foo { static void Main() { XmlSerializer serializer = new XmlSerializer(typeof(Foo)); string xml = @"&lt;foo name='ack' /&gt;"; 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; } } </code></pre> <p>In SP1, the above code runs just fine. In RTM, you get an InvalidOperationException:</p> <blockquote> <p>Unable to generate a temporary class (result=1). error CS0200: Property or indexer 'ConsoleApplication2.Foo.Bar' cannot be assign to -- it is read only</p> </blockquote> <p>Of course, all that's needed to make it run under RTM is adding [XmlIgnore] to the Bar property.</p> <p>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? </p> <p><strong>EDIT</strong>: In SP1, if I added a <code>&lt;Bar /&gt;</code> 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.</p> <p>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.</p> <p><strong>EDIT</strong>: 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:</p> <pre><code>&lt;system.diagnostics&gt; &lt;switches&gt; &lt;add name="XmlSerialization.Compilation" value="1" /&gt; &lt;/switches&gt; &lt;/system.diagnostics&gt; &lt;system.xml.serialization&gt; &lt;xmlSerializer tempFilesLocation="c:\\foo"/&gt; &lt;/system.xml.serialization&gt; </code></pre> <p>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, <a href="http://support.microsoft.com/kb/951847" rel="nofollow noreferrer">change lists</a>, or the <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlignoreattribute.aspx" rel="nofollow noreferrer">XmlIgnoreAttribute documentation</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload