如何将动态对象序列化为XMLc#

How to serialize dynamic object to xml c#(如何将动态对象序列化为XMLc#)

本文介绍了如何将动态对象序列化为XMLc#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个object {System.Collections.Generic.List<object>},其中包含1000个object {DynamicData},每个object {DynamicData}有4个键和值,还有一个List有2个键和值。 我需要将此对象序列化到一个XML文件中,我尝试了正常的序列化,但出现此异常=The type DynamicData was not expected,我如何序列化此对象?

代码如下:

           //output is the name of my object
            XmlSerializer xsSubmit = new XmlSerializer(output.GetType());
            var xml = "";

            using (var sww = new StringWriter())
            {
                using (XmlWriter writers = XmlWriter.Create(sww))
                {
                    try
                    {
                        xsSubmit.Serialize(writers, output);
                    }
                    catch (Exception ex)
                    {

                        throw;
                    }
                    xml = sww.ToString(); // Your XML
                }
            }

我可以逐行和逐元素地创建XML文件,但我想要更快、代码更少的东西。 我的对象的结构如下:

output (count 1000)
 [0]
   Costumer - "Costumername"
   DT - "Date"
   Key - "Key"
   Payment - "x"
   [0]
    Adress - "x"
    Number - "1"
 [1]...
 [2]...

推荐答案

您可以使用IXmlSerializable

实现您自己的序列化对象
[Serializable]
public class ObjectSerialize :  IXmlSerializable
{
    public List<object> ObjectList { get; set; }

    public XmlSchema GetSchema()
    {
        return new XmlSchema();
    }

    public void ReadXml(XmlReader reader)
    {

    }

    public void WriteXml(XmlWriter writer)
    {
        foreach (var obj in ObjectList)
        {   
            //Provide elements for object item
            writer.WriteStartElement("Object");
            var properties = obj.GetType().GetProperties();
            foreach (var propertyInfo in properties)
            {   
                //Provide elements for per property
                writer.WriteElementString(propertyInfo.Name, propertyInfo.GetValue(obj).ToString());
            }
            writer.WriteEndElement();
        }
    }
}

用法

        var output = new List<object>
        {
            new { Sample = "Sample" }
        };
        var objectSerialize = new ObjectSerialize
        {
            ObjectList = output
        };
        XmlSerializer xsSubmit = new XmlSerializer(typeof(ObjectSerialize));
        var xml = "";

        using (var sww = new StringWriter())
        {
            using (XmlWriter writers = XmlWriter.Create(sww))
            {
                try
                {
                    xsSubmit.Serialize(writers, objectSerialize);
                }
                catch (Exception ex)
                {

                    throw;
                }
                xml = sww.ToString(); // Your XML
            }
        }

输出

<?xml version="1.0" encoding="utf-16"?>
<ObjectSerialize>
    <Object>
        <Sample>Sample</Sample>
    </Object>
</ObjectSerialize>
注意:如果要使用相同类型进行反序列化,请务必小心 (对象序列化)您应该提供ReadXml。如果你想 指定架构,也应提供GetSchema

这篇关于如何将动态对象序列化为XMLc#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将动态对象序列化为XMLc#

基础教程推荐