Brilliant :) I figured it was probably something like this - but I had
assumed ReadString and WriteString would exhibit similar behaviour...  it's
a shame the MSDN example shows this too, it's a bit misleading :)

Much thanks Fernando!

 - Alex

> -----Original Message-----
> From: Discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED] On Behalf Of Fernando Tubio
> Sent: Thursday, 7 September 2006 2:05 a.m.
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: [ADVANCED-DOTNET] Fw: IXmlSerializable problem...
>
> I believe the problem is in your IXmlSerializable.ReadXml() code. You are
> using ReadString() which will not consume the end tag. This is not
> noticeable when you are deserializing a single instance, as the element's
> value is correctly read and it doesn't really matter that the reader is
> left
> positioned on the end tag since there is nothing else to read. But when
> you
> place this same class inside a container any members following an element
> of
> this type will not be correctly read.
>
> <MyContainer>
>     <Value1>
>         1
>     </Value1>    <== Reader is positioned here after ReadString
>     <Value2>     <== it should be positioned here
>         2
>     </Value2>
> </MyContainer>
>
> If you substitute ReadString( ) with ReadElementString( ) it should leave
> the reader in the appropiate position after deserializing this member.
>
>     public void ReadXml(System.Xml.XmlReader reader)
>     {
>         _value = reader.ReadElementString();
>     }
>
> Fernando Tubio
>
> ----- Original Message -----
> From: "Alex Henderson" <[EMAIL PROTECTED]>
> Sent: Tuesday, September 05, 2006 8:22 PM
> Subject: Re: IXmlSerializable problem...
>
>
> Nope 2.0 framework... last time I played with IXmlSerializable was with
> the
> 1.1 framework a couple of years back, but I don't think my usage patterns
> were similar (not using the same type twice in a container class) as I
> didn't run into problems... I've tried expelling a schema as well, but
> that
> doesn't seem to have any effect.
>
> I'll probably have to log the generated serialization code and trace it
> manually... :(
>
> Chez,
>
>  - Alex
>
> > -----Original Message-----
> > From: Discussion of advanced .NET topics. [mailto:ADVANCED-
> > [EMAIL PROTECTED] On Behalf Of Patrick Steele
> > Sent: Wednesday, 6 September 2006 10:12 a.m.
> > To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> > Subject: Re: [ADVANCED-DOTNET] IXmlSerializable problem...
> >
> > Yes I did.  I wrestled with it for a few hours and couldn't come up with
> > any reason.  I assume you're using .NET 1.1 and it should be noted that
> > IXmlSerializable is unsupported in 1.1 (it's documented in 2.0 so I hope
> > that means it's now supported for 2.0).
> >
> > My needs were fairly simple so I resorted to creating a derived
> > Hashtable that implemented IXmlSerializable and controlled the XML
> > writing myself.  It worked for my needs but was definitely not a
> > "generic" solution.
> >
> > --
> > Patrick Steele
> > Microsoft .NET MVP
> > http://weblogs.asp.net/psteele
> >
> >
> >
> > -----Original Message-----
> > From: Discussion of advanced .NET topics.
> > [mailto:[EMAIL PROTECTED] On Behalf Of Alex Henderson
> > Sent: Tuesday, September 05, 2006 3:16 AM
> > To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> > Subject: [ADVANCED-DOTNET] IXmlSerializable problem...
> >
> >
> > I've implemented a few classes with custom XML serialization. work's
> > fine, but when a put more then one instance of that class into a
> > container, then serialization works, but when Deserializing only the
> > first instance is worked on - at which point all processing for the
> > remaining elements is abandoned... I'm sure it's probably easy to fix,
> > anyone come across this before?
> >
> > Here's a test fixture to demonstrate my problem:
> >
> >     [TestFixture]
> >     public class SettingsConverterFixture
> >     {
> >         [Test]
> >         public void MyContainerConvert()
> >         {
> >             XmlSerializer serializer = new
> > XmlSerializer(typeof(MyContainer));
> >
> >             MemoryStream stream = new MemoryStream();
> >
> >             MyContainer original = new MyContainer();
> >             original.Value1 = new CustomValue("1");
> >             original.Value2 = new CustomValue("2");
> >
> >             Assert.AreEqual("1", original.Value1.Value);
> >             Assert.AreEqual("2", original.Value2.Value);
> >
> >             serializer.Serialize(stream, original);
> >
> >             stream.Seek(0, SeekOrigin.Begin);
> >
> >             MyContainer reloaded =
> > (MyContainer)serializer.Deserialize(stream);
> >
> >             Assert.AreEqual("1", reloaded.Value1.Value);
> >             Assert.IsNotNull(reloaded.Value2); // blows up here
> >             Assert.AreEqual("2", reloaded.Value2.Value);
> >         }
> >     }
> >
> >     public class MyContainer
> >     {
> >         public CustomValue Value1;
> >         public CustomValue Value2;
> >     }
> >
> >     public class CustomValue : IXmlSerializable
> >     {
> >         private string _value;
> >
> >         public string Value
> >         {
> >             get { return _value; }
> >         }
> >
> >         public CustomValue(string value)
> >         {
> >             _value = value;
> >         }
> >
> >         public CustomValue()
> >         {
> >         }
> >
> >         #region IXmlSerializable Members
> >
> >         public XmlSchema GetSchema()
> >         {
> >             return null;
> >         }
> >
> >         public void ReadXml(System.Xml.XmlReader reader)
> >         {
> >             _value = reader.ReadString();
> >         }
> >
> >         public void WriteXml(System.Xml.XmlWriter writer)
> >         {
> >             writer.WriteString(_value);
> >         }
> >
> >         #endregion
> >     }
> >
>
> ===================================
> This list is hosted by DevelopMentorR  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to