简体   繁体   中英

Serialize Generic List using XML serializer .net c#

i am trying to serialize generic list getting error when using genaric list object in XML Serializer,

my class for generic List is,

 class CustomContact
    {
        private string[] number = new string[5];
        public string Name { get; set; }
        //public string Number { get; set; }
        public string[] Number
        {
            get { return number; }
            set { number = value; }
        }
       // public string Number1 { get; set; }

        public CustomContact()
        {
        }

        //CTOR that takes in a Contact object and extract the two fields we need (can add more fields)
        public CustomContact( Contact contact)
        {
            Name = contact.DisplayName;
            int count = contact.PhoneNumbers.Count();
            for (int i = 0; i < count; i++)
            {
                if (count > 0 && contact.PhoneNumbers.ElementAt(i).PhoneNumber != null && !string.IsNullOrEmpty(contact.PhoneNumbers.ElementAt(i).PhoneNumber))
                {
                    Number[i] = contact.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
                }
                else
                {
                   Number[i] = "";
                }
            }
            /*var number = contact.PhoneNumbers.FirstOrDefault();
                if (number != null)
                    Number = number.PhoneNumber;
                else
                    Number = "";*/
        }

Object of generic list,

 List<CustomContact> listOfContacts  = new List<CustomContact>();

using object of this generic list class in xml serializer,

   XmlSerializer serializer = new XmlSerializer(typeof(List<CustomContact>)); 
                            using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                            {
                                //CHANGE "App.MyStorage.Items" to where your data is.
                               // serializer.Serialize(xmlWriter, App.con.);
                                serializer.Serialize(xmlWriter, listOfContacts);
                            }

getting error in this line ,

 XmlSerializer serializer = new XmlSerializer(typeof(List<CustomContact>)); 

Error is,

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll

List of string type is working fine how to run generic list here ?

Hopes for your suggestion thanks

COMPLETE ERROR:

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll

Additional information: Could not load file or assembly 'System.Xml.Serialization.debug.resources, Version=4.0.0.0, Culture=en-US, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Keep your customer class like below.

public partial class customer
    {
        public string Name { get; set; }
        public int Number { get; set; }
    }

create class like below

public class SerializeHelperImp
{

    public String SerializeToXmlString<T>(T toStringFromObject)
    {
        return SerializeToXmlString<T>(toStringFromObject, new UTF8Encoding());
    }

    public String SerializeToXmlString<T>(T toStringFromObject, Encoding encoding)
    {
        return DoSerializeToXmlString<T>(toStringFromObject, encoding);
    }

    public String SerializeToXmlString<T>(T toStringFromObject, Encoding encoding,
        XmlSerializerNamespaces namespaces)
    {
        return DoSerializeToXmlString<T>(toStringFromObject, encoding);
    }

    private String DoSerializeToXmlString<T>(T toStringFromObject, Encoding encoding)
    {
        String xmlstream = String.Empty;

        using (MemoryStream ms = new MemoryStream())
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof (T));
            XmlTextWriter xmlWriter = new XmlTextWriter(ms, encoding);

            xmlSerializer.Serialize(xmlWriter, toStringFromObject);
            xmlstream = ByteArrayToString(ms.ToArray(), encoding);
        }

        return xmlstream;
    }
     private String ByteArrayToString(Byte[] arrayOfBytes, Encoding encoding)
    {
        return encoding.GetString(arrayOfBytes);
    }
}

You can write a method like below.

private String SerializeToXmlString<T>(T objectToSerialise)
        {
            var serializeHelper = new SerializeHelperImp();
            return serializeHelper.SerializeToXmlString<T>(objectToSerialise, new UTF8Encoding());
        }

and then call the above method like below and pass your object.

customer customerobject = new customer() { Name = "abc1",Number = 23};
 var stockServiceAsXmlString = SerializeToXmlString<customer>(customerobject);

working solution for this can be found here . Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM