dcsimg
Results 1 to 3 of 3

Thread: XMLSerializer

  1. #1

    Thread Starter
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    144

    XMLSerializer

    Hello,

    I have a problem with XMLSerialization.
    I have a small class with 2 string properties. like shown below

    Code:
    namespace License.Message.BodyInformation
    {
        using System;
        using System.Text;
    
        /// <summary>
        /// Class WhoIsStatus.
        /// </summary>
        [Serializable]
        public class WhoIsStatus
        {
            /// <summary>
            /// The host
            /// </summary>
            private string host;
    
            /// <summary>
            /// The user
            /// </summary>
            private string user;
    
            /// <summary>
            /// Initializes a new instance of the <see cref="WhoIsStatus" /> class.
            /// </summary>
            public WhoIsStatus() 
            {
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref="WhoIsStatus" /> class.
            /// </summary>
            /// <param name="host">The host.</param>
            /// <param name="user">The user.</param>
            public WhoIsStatus(string host, string user) : base()
            {
                this.Host = host;
                this.User = user;
            }
    
            /// <summary>
            /// Gets or sets the host.
            /// </summary>
            /// <value>The host.</value>
            public string Host
            {
                get
                {
                    return this.host;
                }
                set
                {
                    this.host = value;
                }
            }
    
            /// <summary>
            /// Gets or sets the user.
            /// </summary>
            /// <value>The user.</value>
            public string User
            {
                get
                {
                    return this.user;
                }
                set
                {
                    this.user = value;
                }
            }
    
            /// <summary>
            /// Returns a <see cref="System.String" /> that represents this instance.
            /// </summary>
            /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
            public override string ToString()
            {
                Console.WriteLine(License.Message.IO.Serializer.Save(this,Encoding.ASCII));
                
                return License.Message.IO.Serializer.Save(this, Encoding.ASCII);
            }
    
            /// <summary>
            /// Constructs a message from an XML string
            /// </summary>
            /// <param name="value">XML string that holds a message structure</param>
            public void FromString(string value)
            {
                try
                {
                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        WhoIsStatus who = License.Message.IO.Serializer.Load<WhoIsStatus>(value);
                        this.User = who.User;
                        this.Host = who.Host;
                    }
                }
                catch (Exception)
                {
                }
            }
        }
    }
    HTML Code:
    <?xml version="1.0"?>
    <WhoIsStatus xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Host>Valhalla</Host>
      <User>MasterChief</User>
    </WhoIsStatus>????????????????????????????????????????????????????
    I was wondering where all the question marks come from.
    When i add 2 extra properties (Depending on the length of the name) they disappear.

    The XMLSerializer class i use, is as follows.

    Code:
    namespace License.Message.IO
    {
        using System.IO;
        using System.Text;
        using System.Xml;
        using System.Xml.Serialization;
    
        /// <summary>
        /// Based on work by Simon Bridge, May 2011 mailto:srbridge@gmail.com
        /// This code Is provided under the Code Project Open License (CPOL)
        /// See www.codeproject.com/info/cpol10.aspx for details
        /// </summary>
        public static class Serializer
        {
            #region Public Method(s)
            /// <summary>
            /// Save's T to an XML file
            /// </summary>
            /// <typeparam name="T">type of object</typeparam>
            /// <param name="o">the object</param>
            /// <param name="fileName">the filename</param>
            public static void Save<T>(T o, string fileName)
            {
                using (FileStream fs = File.Open(fileName, FileMode.Create))
                {
                    Save<T>(o, fs);
                }
            }
    
            /// <summary>
            /// Save's T to an XML file
            /// </summary>
            /// <typeparam name="T">type of object</typeparam>
            /// <param name="o">the object</param>
            /// <param name="stream">the stream</param>
            public static void Save<T>(T o, Stream stream)
            {
                GetSerializer<T>().Serialize(stream, o);
            }
    
            /// <summary>
            /// Save's T to an XML file
            /// </summary>
            /// <typeparam name="T">type of object</typeparam>
            /// <param name="o">the object</param>
            /// <returns>a byte array</returns>
            public static byte[] Save<T>(T o)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    Save<T>(o, ms);
                    return ms.GetBuffer();
                }
            }
    
            /// <summary>
            /// Save's T to an XML file
            /// </summary>
            /// <typeparam name="T">type of object</typeparam>
            /// <param name="o">the object</param>
            /// <param name="encoding">System Text Encoding</param>
            /// <returns>a string</returns>
            public static string Save<T>(T o, Encoding encoding)
            {
                return encoding.GetString(Save<T>(o));
            }
    
            /// <summary>
            /// Loads the object from an XML structure
            /// </summary>
            /// <typeparam name="T">type of object</typeparam>
            /// <param name="stream">stream containing the XML structure</param>
            /// <returns>an object of type T</returns>
            public static T Load<T>(Stream stream)
            {
                return (T)GetSerializer<T>().Deserialize(stream);
            }
    
            /// <summary>
            /// Loads the object from an XML structure
            /// </summary>
            /// <typeparam name="T">type of object</typeparam>
            /// <param name="file">file containing the XML structure</param>
            /// <returns>an object of type T</returns>
            public static T Load<T>(FileInfo file)
            {
                using (FileStream fs = file.Open(FileMode.Open))
                {
                    return Load<T>(fs);
                }
            }
    
            /// <summary>
            /// Loads the object from an XML structure
            /// </summary>
            /// <typeparam name="T">type of object</typeparam>
            /// <param name="data">byte array containing the XML structure</param>
            /// <returns>an object of type T</returns>
            public static T Load<T>(byte[] data)
            {
                using (MemoryStream ms = new MemoryStream(data))
                {
                    return Load<T>(ms);
                }
            }
    
            /// <summary>
            /// Loads the object from an XML structure
            /// </summary>
            /// <typeparam name="T">type of object</typeparam>
            /// <param name="data">string containing the XML structure</param>
            /// <returns>an object of type T</returns>
            public static T Load<T>(string data)
            {
                return Load<T>(Encoding.ASCII.GetBytes(data));
            }
    
            /// <summary>
            /// Loads the specified data.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="data">The data.</param>
            /// <param name="encoding">The encoding.</param>
            /// <returns>T.</returns>
            public static T Load<T>(string data,Encoding encoding)
            {
                return Load<T>(encoding.GetBytes(data));
            }
    
            /// <summary>
            /// Loads the object from an XML structure
            /// </summary>
            /// <typeparam name="T">type of object</typeparam>
            /// <param name="doc">XML document holding the information</param>
            /// <returns>an object of type T</returns>
            public static T Load<T>(XmlDocument doc)
            {
                return Load<T>(doc.ToString());
            }
            #endregion
            #region Private Method(s)
            /// <summary>
            /// The serializer that does all the work.
            /// </summary>
            /// <typeparam name="T">object type</typeparam>
            /// <returns>serializer of type T</returns>
            private static XmlSerializer GetSerializer<T>()
            {
                return new XmlSerializer(typeof(T));
            }
    #endregion
        }
    }
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  2. #2

    Thread Starter
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    144

    Re: XMLSerializer

    Delete double post
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3

    Thread Starter
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    144

    Re: XMLSerializer

    Ok, a little update:

    The questionmarks @ the end is solved by replacing the .GetBuffer methode of the MemoryStream with .ToArray. The .GetBuffer returns chuncs of 256 Bits.

    See: https://docs.microsoft.com/en-us/dot...ream_GetBuffer

    But now i have an other problem.

    When i serialize an serialized object, some caracters are changed. for an example "<" is changed into &lt.
    This is preventing the XML from being deserialized back into the object again.
    How can i prevent this behaviour?
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width