简体   繁体   English

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

[英]How to serialize dynamic object to xml c#

I have a object {System.Collections.Generic.List<object>} that contains 1000 object {DynamicData} inside of it, each one with 4 keys and values and one more List with 2 keys and values inside. 我有一个object {System.Collections.Generic.List<object>} ,其中包含1000个object {DynamicData} ,每个object {DynamicData}有4个键和值,还有一个List ,里面有2个键和值。 I need to serialize this object into a XML File, i tried normal serialization but it gives me this exception = The type DynamicData was not expected , how can i serialize this object? 我需要将此对象序列化为XML文件,我尝试了正常的序列化但它给了我这个异常= The type DynamicData was not expected ,我如何序列化这个对象?

Here is the code: 这是代码:

           //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
                }
            }

I can create the xml file writing line by line and element by element, but i want something more faster and with less code. 我可以创建逐行和逐元素地写入xml文件,但我想要更快更快的代码。 The structure of my object is like this: 我的对象的结构是这样的:

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

You can implement your own serialize object by using IXmlSerializable 您可以使用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();
        }
    }
}

Usage ; 用法 ;

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

Output 产量

<?xml version="1.0" encoding="utf-16"?>
<ObjectSerialize>
    <Object>
        <Sample>Sample</Sample>
    </Object>
</ObjectSerialize>

Note : Be careful with that, if you want to deserialize with same type (ObjectSerialize) you should provide ReadXml . 注意:请注意,如果要使用相同类型(ObjectSerialize)进行反序列化,则应提供ReadXml And if you want to specify schema, you should provide GetSchema too. 如果要指定模式,则还应提供GetSchema

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

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