none
How to serialize/ deserialize xml to/from string RRS feed

  • Question

  • I have stored xml in string variable. i could write a program which can load xml from string and deserialize to class but i need to know how could i serialize my class to xml and store the xml into string variable. please add some code in my program to show how could i serialize my class to xml and store the xml into string variable.

    here is my program

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Xml.Serialization;
    using System.Xml;
    using System.IO;

    namespace List2XML
    {
        public partial class Form1 : Form
        {
            string _xml = @"<?xml version=""1.0"" ?>
            <CustomerQueryRs>
            <CustomerRet>
            <ListID>9999999bbbb</ListID>
            <Name>Zlaine Bailey</Name>
            <FullName>Zlaine Bailey</FullName>
            <Phone>33333333</Phone>
            </CustomerRet>
            <CustomerRet>
            <ListID>9AAAAAA</ListID>
            <Name>+ Brian Boyd</Name>
            <FullName>Brian Boyd</FullName>
            <Phone>6666666666</Phone>
            </CustomerRet>
            <CustomerRet>
            <ListID>92MMMMMMMMMMM</ListID>
            <Name>Moni Leahy</Name>
            <FullName>Moni Leahy</FullName>
            <Phone>1111111111111</Phone>
            </CustomerRet>
            </CustomerQueryRs>";


            public Form1()
            {
                InitializeComponent();
            }

            private void btnSerialize_Click(object sender, EventArgs e)
            {

            }

            private void btnDeserialize_Click(object sender, EventArgs e)
            {
                //XmlDocument doc = new XmlDocument();
                //doc.LoadXml(_xml);
                var CustomerList = DeserialzeXml(_xml);
                //dataGridView1.DataSource = CustomerList;
            }

            private object DeserialzeXml(string xml)
            {

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);
                XmlSerializer xs = new XmlSerializer(typeof(CustomerList));
                CustomerList cl = (CustomerList)xs.Deserialize(new StringReader(doc.OuterXml));
                return cl.Customers;
            }
        }


        [XmlRoot("CustomerQueryRs")]
        public class CustomerList
        {

            [XmlElement("CustomerRet")]
            public List<Customer> Customers
            {
                get { return customers; }
                set { customers = value; }
            }

            private List<Customer> customers = null;

            public CustomerList()
            {
                customers = new List<Customer>();
            }
        }

        public class Customer
        {
            [XmlElement(ElementName = "ListID")]
            public string ListID { get; set; }

            [XmlElement(ElementName = "Name")]
            public string Name { get; set; }

            [XmlElement(ElementName = "FullName")]
            public string FullName { get; set; }

            [XmlElement(ElementName = "Phone")]
            public string Phone { get; set; }
        }
    }

    Wednesday, August 15, 2018 6:01 PM

Answers

  • Hi Sudip_inn,

    Thank you for posting here.

    >>but i need to know how could i serialize my class to xml and store the xml into string variable. 

    For your question, if you want to serialize xml and store xml into string variable, please try the code below.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    
    namespace ConsoleApp
    {
        class Serialize_XML_string
        {
            static void Main(string[] args)
            {
    
                List<CustomerQueryRsCustomerRet> RSlist = new List<CustomerQueryRsCustomerRet>();
                CustomerQueryRsCustomerRet Ret = new CustomerQueryRsCustomerRet();
                Ret.ListID = "9999999bbbb";
                Ret.Name = "Zlaine Bailey";
                Ret.FullName = "Zlaine Bailey";
                Ret.Phone = 33333333;
                RSlist.Add(Ret);
    
                CustomerQueryRsCustomerRet Ret2 = new CustomerQueryRsCustomerRet();
                Ret.ListID = "9AAAAAA";
                Ret.Name = "+ Brian Boyd";
                Ret.FullName = "Brian Boyd";
                Ret.Phone = 6666666666;
                RSlist.Add(Ret2);
    
                CustomerQueryRsCustomerRet Ret3 = new CustomerQueryRsCustomerRet();
                Ret.ListID = "92MMMMMMMMMMM";
                Ret.Name = "Moni Leahy";
                Ret.FullName = "Moni Leahy";
                Ret.Phone = 1111111111111;
                RSlist.Add(Ret3);
    
                string s = Serialize(RSlist);
    
    
            }
            public static string Serialize(List<CustomerQueryRsCustomerRet> list)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<CustomerQueryRsCustomerRet>));
                using (StringWriter textWriter = new StringWriter())
                {
                    serializer.Serialize(textWriter, list);
                    return textWriter.ToString();
                }
            }
        }
        // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
        /// <remarks/>
        [Serializable()]
        [DesignerCategory("code")]
        [System.Xml.Serialization.XmlType(AnonymousType = true)]
        [System.Xml.Serialization.XmlRoot(Namespace = "", IsNullable = false)]
        public partial class CustomerQueryRs
        {
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElement("CustomerRet")]
            public CustomerQueryRsCustomerRet[] CustomerRet { get; set; }
        }
    
        /// <remarks/>
        [Serializable()]
        [DesignerCategory("code")]
        [System.Xml.Serialization.XmlType(AnonymousType = true)]
        public partial class CustomerQueryRsCustomerRet
        {
    
            /// <remarks/>
            public string ListID { get; set; }
    
            /// <remarks/>
            public string Name { get; set; }
    
            /// <remarks/>
            public string FullName { get; set; }
    
            /// <remarks/>
            public ulong Phone { get; set; }
        }
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, August 16, 2018 5:52 AM
    Thursday, August 16, 2018 3:48 AM
    Moderator