By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

IXmlSerialization problem

P: 2
Hi, can anyone help me out with this problem? In the code posted below, why does my count of Data objects only return 1, not 3. If I comment out the IXmlSerializable inheritance in the Data class, thereby relying on the built in Xml Serialization rather than my own implementation, I get 3 as expected.

BTW, this is a distilled example of the problem - I need to implement IXmlSerializable as I have to hook up some event handlers following deserialization.

Thanks in advance, sorry for long code!

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Serialization;
  6. using System.Xml;
  7. using System.IO;
  8.  
  9. namespace SerializationTest
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Data[] input = new Data[3];
  16.             input[0] = new Data("one");
  17.             input[1] = new Data("two");
  18.             input[2] = new Data("three");
  19.  
  20.             Holder holder = new Holder("holder1");
  21.             holder.DataSeries = input;
  22.  
  23.             TextWriter writer = new StreamWriter("E:\\Stuff\\test.xml");
  24.             XmlSerializer sz = new XmlSerializer(typeof (Holder));
  25.             sz.Serialize(writer, holder);
  26.             writer.Flush();
  27.             writer.Close();
  28.  
  29.             FileStream reader = File.OpenRead("E:\\Stuff\\test.xml");
  30.             XmlSerializer sz2 = new XmlSerializer(typeof (Holder));
  31.             Holder result = (Holder) sz2.Deserialize(reader);
  32.             Console.WriteLine("Number of data objects in series: {0}", result.DataSeries.Length);
  33.             reader.Close();
  34.         }
  35.     }
  36.  
  37.     [Serializable]
  38.     public class Holder
  39.     {
  40.         private string _name;
  41.         private Data[] _dataSeries;
  42.  
  43.         public Holder() { }
  44.  
  45.         public Holder(string name)
  46.         {
  47.             _name = name;
  48.         }
  49.  
  50.         [XmlAttribute("HolderName")]
  51.         public string Name
  52.         {
  53.             get { return _name; }
  54.             set { _name = value; }
  55.         }
  56.  
  57.         [XmlArray]
  58.         public Data[] DataSeries
  59.         {
  60.             get { return _dataSeries; }
  61.             set { _dataSeries = value; }
  62.         }
  63.     }
  64.  
  65.     [Serializable]
  66.     public class Data : IXmlSerializable
  67.     {
  68.         private string _dataName;
  69.  
  70.         public Data() { }
  71.  
  72.         public Data(string name)
  73.         {
  74.             _dataName = name;
  75.         }
  76.  
  77.         public string DataName
  78.         {
  79.             get { return _dataName; }
  80.             set { _dataName = value; }
  81.         }
  82.  
  83.         #region IXmlSerializable Members
  84.  
  85.         public System.Xml.Schema.XmlSchema GetSchema()
  86.         {
  87.             return null;
  88.         }
  89.  
  90.         public void ReadXml(XmlReader reader)
  91.         {
  92.             reader.ReadToFollowing("DataName");
  93.             _dataName = reader.ReadElementContentAsString("DataName", string.Empty);
  94.         }
  95.  
  96.         public void WriteXml(XmlWriter writer)
  97.         {
  98.             writer.WriteElementString("DataName", _dataName);
  99.         }
  100.  
  101.         #endregion
  102.     }
  103.  
  104. }
Jan 29 '09 #1
Share this Question
Share on Google+
2 Replies


vekipeki
Expert 100+
P: 229
Just read the end </DataName> element before returning from ReadXml.

Expand|Select|Wrap|Line Numbers
  1. public void ReadXml(XmlReader reader)
  2. {
  3.     reader.ReadToFollowing("DataName");
  4.     _dataName = reader.ReadElementContentAsString("DataName", string.Empty);
  5.     reader.ReadEndElement();
  6. }
Jan 30 '09 #2

P: 2
Brilliant! Thanks very much for that - it works now.
Jan 30 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.