English 中文(简体)
如何编订清单
原标题:How to serialize 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; }
}

首先是罚款。 如果我穿过任何种类的话,它正在成功地恢复Xml的扼杀。

CORRECTION: 汇编错误出现在第二版(Error:不能从List<MyType>List<object>

我将第二版改写,解决我的问题。 如今,该编码正在编号<代码>。 List<generic categories>。

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;
}
最佳回答
问题回答

我曾尝试过你的两项职能,没有太多麻烦。 我改变的唯一一点是:

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

为此:

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

甲型六氯环己烷需要一种实际类型,而GetType()则将物体类型退回。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签