dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: DeSerializing Not Working!!!!

  1. #1
    Join Date
    Apr 2004
    Posts
    123

    DeSerializing Not Working!!!!

    Hi,

    In the below mentioned code,I'm trying to deserialize the given XML into an object graph of type Message.Find the code below...

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml.Serialization;
    namespace TestXML
    {
      public class Program
        {
            static void Main(string[] args)
            {
                string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Message><Books><Book><Author>Jack</Author><State>CA</State><Code><ISBN>1111-2222</ISBN><Price>35</Price></Code></Book></Books><Books>	<Book><Author>Daniel</Author><State>IL</State><Code><ISBN>3333-5555</ISBN><Price>23</Price></Code></Book></Books></Message>";
    
                XmlSerializer ser = new XmlSerializer(typeof(Message));
    
                using (System.IO.StringReader reader = new System.IO.StringReader(xml))
                {
                    Message msg = ser.Deserialize(reader) as Message;
    
                }
    
            }
    
            [Serializable()]
            public class Message
            {
                public Message()
                {
    
                }
               
                public Books[] Books;
            }
    
            [Serializable()]
            public class Books
            {
                public Books()
                {
    
                }
    
                public Book Book;
            }
    
            [Serializable()]
            public class Book
            {
                public Book()
                {
    
                }
    
                public Code Code;
    
            }
    
            [Serializable()]
            public class Code
            {
                public Code()
                {
    
                }
            }
        }
    }
    But after deserializing,the msg instance is not having any collection of Books....It just shows 0 dimension of books.Why is it happening so...??

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: DeSerializing Not Working!!!!

    Try this:
    Code:
    [Serializable()]
    		public class Message
    		{
    			public Message()
    			{
    
    			}
               
    			[XmlArray("Books")]
    			[XmlArrayItem(typeof(Book))]
    			public Book[] Books;
    		}
    
    		[Serializable()]
    		public class Book
    		{
    			public Book()
    			{
    
    			}
    
    			[XmlElement]
    			public string Author;
    
    			[XmlElement]
    			public Code Code;
    		}
    
    		[Serializable()]
    		public class Code
    		{
    			public Code()
    			{
    
    			}
    
    			[XmlElement("ISBN")]
    			public string isbn;
    
    			[XmlElement("Price")]
    			public string price;
    		}
    Important points: Books class is wiped out; look at the way in which can be name of xml element defined different from code element (Code class).
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Apr 2004
    Posts
    123

    Re: DeSerializing Not Working!!!!

    Thanks for the quick response...

    I see that you have refactored Books...absolutely no issues with it...But if you look at the XML...there is 2 nodes for Books...

    Now...If i deserialize using the code which you have mentioned...only one <Books> is present...or in other words...I'm losing JACK...only DANIELS is deserialized...or after deserializing the length of books is 1 wherin it should be 2...

    Any thoughts/suggestions/solutions on this....???

  4. #4
    Join Date
    Aug 2007
    Location
    Minneapolis
    Posts
    155

    Post Re: DeSerializing Not Working!!!!

    Have you tried just serializing/deserializing the object(s) instead of mapping/unmapping them to XML. That, however, may not be an option, if serializer and deserializer both don't have access to the library supporting the Books class, but if they do, you could save yourself a lot of trouble. Here's a sample you could look into.
    Code:
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Runtime.Serialization.Formatters.Soap;
    namespace TestXML
    {
        public class Program
        {
            private const string DATA_FILE = @"C:\Documents and Settings\All Users\Desktop\message.xml";
            static void Main(string[] args)
            {
                // serialize books
                Books books = new Books();
                books.Add(new Book("Jack", "CA", new Code("1111-2222", 35.0)));
                books.Add(new Book("Daniel", "IL", new Code("3333-5555", 23.0)));
                new Program().SerializeMessage(books);
    
                // deserialize the books message
                Books deserializedBooks = new Program().DeseralizeMessage();
            }
            private void SerializeMessage(Books message)
            {
                using (Stream stream = File.Open(DATA_FILE, FileMode.Create))
                {
                    SoapFormatter formatter = new SoapFormatter();
                    formatter.Serialize(stream, message);
                    stream.Close();
                }
            }
            private Books DeseralizeMessage()
            {
                Books message = null;
                using (Stream stream = File.Open(DATA_FILE, FileMode.Open))
                {
                    SoapFormatter formatter = new SoapFormatter();
                    message = (Books)formatter.Deserialize(stream);
                    stream.Close();
                }
                return message;
            }
        }
        [Serializable()]
        internal sealed class Books : List<Book> { }
        [Serializable()]
        internal sealed class Book
        {
            private string _author;
            private string _state;
            private Code _code;
            public Book(string author, string state, Code code)
            {
                _author = author;
                _state = state;
                _code = code;
            }
        }
        [Serializable()]
        internal sealed class Code
        {
            private string _isbn;
            private double _price;
            public Code(string isbn, double price)
            {
                _isbn = isbn;
                _price = price;
            }
        }
    }
    For this to work, you would have to add the .Net refence, System.Runtime.Serialization.Formatters.Soap.dll and you could add the necessary properties to Book and Code classes as you see fit and change their access modifiers as necessary.

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: DeSerializing Not Working!!!!

    Quote Originally Posted by mmx_nexus
    Thanks for the quick response...

    I see that you have refactored Books...absolutely no issues with it...But if you look at the XML...there is 2 nodes for Books...

    Now...If i deserialize using the code which you have mentioned...only one <Books> is present...or in other words...I'm losing JACK...only DANIELS is deserialized...or after deserializing the length of books is 1 wherin it should be 2...

    Any thoughts/suggestions/solutions on this....???
    try to use list/collection instead of array on the Books property.

    Code:
    [Serializable()]
    		public class Message
    		{
    			public Message()
    			{
    
    			}
                             public List<Book> Books;
               
    			/*[XmlArray("Books")]
    			[XmlArrayItem(typeof(Book))]
    			public Book[] Books;*/
    		}
    
    		[Serializable()]
    		public class Book
    		{
    			public Book()
    			{
    
    			}
    
    			[XmlElement]
    			public string Author;
    
    			[XmlElement]
    			public Code Code;
    		}
    
    		[Serializable()]
    		public class Code
    		{
    			public Code()
    			{
    
    			}
    
    			[XmlElement("ISBN")]
    			public string isbn;
    
    			[XmlElement("Price")]
    			public string price;
    		}
    Busy

  6. #6
    Join Date
    Apr 2004
    Posts
    123

    Re: DeSerializing Not Working!!!!

    Guys,

    The solution of using a generic List works like a charm!!!Thanks for saving my day...

    But,still the original question remains...why is that only Daniels is getting picked up when the array of books is supposed to have both.Is it a problem with Arrays..???

    Mmx

  7. #7
    Join Date
    Aug 2007
    Location
    Minneapolis
    Posts
    155

    Re: DeSerializing Not Working!!!!

    Make sure you cover the full index range of the array, 0 through length-1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)