Convert an object to an XML string

Convert an object to an XML string

Asked on January 12, 2019 in XML.
Add Comment


  • 9 Answer(s)

         The conversion method for object to xml. ‘this‘ is instance of the class.

    public string ToXML()
       {
          var stringwriter = new System.IO.StringWriter();
          var serializer = new XmlSerializer(this.GetType());
          serializer.Serialize(stringwriter, this);
          return stringwriter.ToString();
       }
     
    public static YourClass LoadFromXMLString(string xmlText)
       {
          var stringReader = new System.IO.StringReader(xmlText);
          var serializer = new XmlSerializer(typeof(YourClass ));
          return serializer.Deserialize(stringReader) as YourClass ;
       }
    

     

    Answered on January 12, 2019.
    Add Comment

         To convert an object to xml string, try the given code:

    public static string Serialize<T>(T dataToSerialize)
    {
       try
       {
          var stringwriter = new System.IO.StringWriter();
          var serializer = new XmlSerializer(typeof(T));
          serializer.Serialize(stringwriter, dataToSerialize);
          return stringwriter.ToString();
       }
       catch
       {
          throw;
       }
    }
     
    public static T Deserialize<T>(string xmlText)
    {
       try
       {
          var stringReader = new System.IO.StringReader(xmlText);
          var serializer = new XmlSerializer(typeof(T));
          return (T)serializer.Deserialize(stringReader);
       }
       catch
       {
          throw;
       }
    }
    

         All these methods can be placed in a static helper class at present, which means no duplication code to all class that wants to be serialized.

    Answered on January 12, 2019.
    Add Comment

    Converting object to xml:

    public static string Serialize(object dataToSerialize)
    {
       if(dataToSerialize==null) return null;
     
       using (StringWriter stringwriter = new System.IO.StringWriter())
       {
          var serializer = new XmlSerializer(dataToSerialize.GetType());
          serializer.Serialize(stringwriter, dataToSerialize);
          return stringwriter.ToString();
       }
    }
     
    public static T Deserialize<T>(string xmlText)
    {
       if(String.IsNullOrWhiteSpace(xmlText)) return default(T);
     
       using (StringReader stringReader = new System.IO.StringReader(xmlText))
       {
          var serializer = new XmlSerializer(typeof(T));
          return (T)serializer.Deserialize(stringReader);
       }
    }
    
    Answered on January 12, 2019.
    Add Comment

    Here are conversion method for both ways. this = instance of your class

    public string ToXML()
    {
    using(var stringwriter = new System.IO.StringWriter())
    {
    var serializer = new XmlSerializer(this.GetType());
    serializer.Serialize(stringwriter, this);
    return stringwriter.ToString();
    };
    }

    public static YourClass LoadFromXMLString(string xmlText)
    {
    using(var stringReader = new System.IO.StringReader(xmlText))
    {
    var serializer = new XmlSerializer(typeof(YourClass ));
    return serializer.Deserialize(stringReader) as YourClass ;
    };
    }

    Answered on February 5, 2019.
    Add Comment
    XmlSerializer serializer = new XmlSerializer(typeof(MethodCheckType));
            MethodCheckType output = null;
            StringReader reader = null;
    
            // catch global exception, logg it and throw it
            try
            {
                reader = new StringReader(path);
                output = (MethodCheckType)serializer.Deserialize(reader);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                reader.Dispose();
            }
    
            return output.WebService;
    Answered on February 25, 2019.
    Add Comment
    ublic string ToXML()
        {
            using(var stringwriter = new System.IO.StringWriter())
            { 
                var serializer = new XmlSerializer(this.GetType());
                serializer.Serialize(stringwriter, this);
                return stringwriter.ToString();
            };
        }
    
     public static YourClass LoadFromXMLString(string xmlText)
        {
            using(var stringReader = new System.IO.StringReader(xmlText))
            {
                var serializer = new XmlSerializer(typeof(YourClass ));
                return serializer.Deserialize(stringReader) as YourClass ;
            };
        }
    Answered on February 25, 2019.
    Add Comment
    public static string Serialize<T>(T dataToSerialize)
    {
        try
        {
            var stringwriter = new System.IO.StringWriter();
            var serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(stringwriter, dataToSerialize);
            return stringwriter.ToString();
        }
        catch
        {
            throw;
        }
    }
    
    public static T Deserialize<T>(string xmlText)
    {
        try
        {
            var stringReader = new System.IO.StringReader(xmlText);
            var serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stringReader);
        }
        catch
        {
            throw;
        }
    }
    Answered on February 25, 2019.
    Add Comment
    public static string Serialize(object dataToSerialize)
        {
            if(dataToSerialize==null) return null;
    
            using (StringWriter stringwriter = new System.IO.StringWriter())
            {
                var serializer = new XmlSerializer(dataToSerialize.GetType());
                serializer.Serialize(stringwriter, dataToSerialize);
                return stringwriter.ToString();
            }
        }
    
        public static T Deserialize<T>(string xmlText)
        {
            if(String.IsNullOrWhiteSpace(xmlText)) return default(T);
    
            using (StringReader stringReader = new System.IO.StringReader(xmlText))
            {
                var serializer = new XmlSerializer(typeof(T));
                return (T)serializer.Deserialize(stringReader);
            }
    Answered on February 25, 2019.
    Add Comment
    public string BuildXml<T>(ICollection<T> anyObject, string keyFather, string keySon)
        {
            var settings = new XmlWriterSettings
            {
                Indent = true
            };
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
            StringBuilder builder = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(builder, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement(keyFather);
                foreach (var objeto in anyObject)
                {
                    writer.WriteStartElement(keySon);
                    foreach (PropertyDescriptor item in props)
                    {
                        writer.WriteStartElement(item.DisplayName);
                        writer.WriteString(props[item.DisplayName].GetValue(objeto).ToString());
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }
                writer.WriteFullEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                return builder.ToString();
            }
        }
    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.