简体   繁体   English

如何序列化List <object>

[英]How to serialize List<object>

I am writing common functions to serialize the given object and List<object> as follows 我正在编写常用函数来序列化给定对象和List <object>,如下所示

public string SerializeObject(Object pObject)// for given object
{
    try
    {
        String XmlizedString = null;
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(typeof(pObject));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        xs.Serialize(xmlTextWriter, pObject);
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        return XmlizedString;
    }
    catch (Exception e) { System.Console.WriteLine(e); return null; }
}

public string SerializeObject(List<Object> pObject)// for given List<object>
{
    try
    {
        String XmlizedString = null;
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(typeof(pObject));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        xs.Serialize(xmlTextWriter, pObject);
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        return XmlizedString;
    }
    catch (Exception e) { System.Console.WriteLine(e); return null; }
}

first one is working fine. 第一个工作正常。 If I pass any type, it is successfully returning xml string. 如果我传递任何类型,它会成功返回xml字符串。

CORRECTION: Compilation error has occurred for second one (Error: cannot convert from List<MyType> to List<object> . 更正:第二次出现编译错误(错误:无法从List<MyType>转换为List<object>

I rewrite the second one as follows which solves my problem. 我重写第二个如下解决了我的问题。 Now it is serializing the given List<generic types> . 现在它正在序列化给定的List<generic types>

private string SerializeObject<T>(T source)
{
    MemoryStream memoryStream = new MemoryStream();
    XmlSerializer xs = new XmlSerializer(typeof(T));
    XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
    xs.Serialize(xmlTextWriter, source);
    memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
    string XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
    return XmlizedString;
}

https://weblogs.asp.net/rajbk/Contents/Item/Display/345 https://weblogs.asp.net/rajbk/Contents/Item/Display/345

The relevant code from the article: 文章中的相关代码:

private static string SerializeObject<T>(T source)
{
    var serializer = new XmlSerializer(typeof(T));
    using (var sw = new System.IO.StringWriter())
    using (var writer = XmlWriter.Create(sw))
    {
        serializer.Serialize(writer, source);
        return sw.ToString();
    }
}

I have tried your two functions without much trouble. 我已经尝试了你的两个功能,没有太多麻烦。 The only thing I changed was this line: 我唯一改变的就是这一行:

XmlSerializer xs = new XmlSerializer(typeof(pObject));

to this: 对此:

XmlSerializer xs = new XmlSerializer(pObject.GetType());

typeof() requires an actual type whereas GetType() returns the type of the object. typeof()需要实际类型,而GetType()返回对象的类型。

protobuf 和列表<object> - 如何序列化/反序列化?<div id="text_translate"><p> 我有一个List&lt;object&gt; ,其中包含不同类型的对象,例如整数、字符串和自定义类型。 所有自定义类型都经过 protobuf 调整。 我现在想做的是用 protobuf.net 序列化/反序列化这个列表。 到目前为止,我怀疑我必须显式声明每种类型,不幸的是,这些混合列表构造不可能做到这一点。 因为二进制格式化程序在做这些事情时没有问题,我希望我错过了一些东西,你可以帮助我。 所以我的问题是如何处理 protobuf.net 中的对象。</p></div></object> - protobuf and List<object> - how to serialize / deserialize?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何序列化列表 <List<object> &gt;? - How to serialize List<List<object>>? 如何序列化动态对象的列表属性 - How to serialize list property of a dynamic object protobuf 和列表<object> - 如何序列化/反序列化?<div id="text_translate"><p> 我有一个List&lt;object&gt; ,其中包含不同类型的对象,例如整数、字符串和自定义类型。 所有自定义类型都经过 protobuf 调整。 我现在想做的是用 protobuf.net 序列化/反序列化这个列表。 到目前为止,我怀疑我必须显式声明每种类型,不幸的是,这些混合列表构造不可能做到这一点。 因为二进制格式化程序在做这些事情时没有问题,我希望我错过了一些东西,你可以帮助我。 所以我的问题是如何处理 protobuf.net 中的对象。</p></div></object> - protobuf and List<object> - how to serialize / deserialize? 如何序列化对象以生成子列表? - How to serialize an object to generate a list of childs? 指定如何将对象序列化,如果它在列表中 - Specify how to Serialize my object if it is in a list 如何使用自定义对象的类型序列化列表列表? - How to serialize a list of lists with the type of custom object? 如何序列化/反序列化List <class object> 与protobuf? - how to serialize / deserialize List<class object> with protobuf? 如何序列化自定义对象类型的列表 - How to serialize a List of custom object types 如何序列化列表 <object> 有专用于JSON的名称? - How to serialize List<object> with dedicated name to JSON? 如何使用对象属性序列化CustomClass列表 - how to serialize List of CustomClass with object attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM