使用XML序列化程序序列化通用列表.net c#

时间:2014-05-29 10:14:59

标签: c# asp.net .net xml-serialization generic-list

我试图在XML Serializer中使用genaric list object时序列化通用列表获取错误,

我的通用列表类是,

 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 = "";*/
        }

通用列表的对象,

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

在xml序列化程序中使用此通用列表类的对象,

   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);
                            }

在此行中收到错误,

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

错误是,

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

字符串类型列表工作正常如何在这里运行通用列表?

希望你的建议谢谢

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.

1 个答案:

答案 0 :(得分:1)

保持您的客户类如下。

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

创建类如下

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);
    }
}

您可以编写如下方法。

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

然后像下面那样调用上面的方法并传递你的对象。

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

working solution for this can be found here。希望这会对你有所帮助。