XmlSerializer - XmlWriter - 带对象的数组 [英] XmlSerializer - XmlWriter - Array with object

查看:47
本文介绍了XmlSerializer - XmlWriter - 带对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

推荐答案

Markus Freitag,

Hi Markus Freitag,

感谢您在此发帖。

对于您的问题,我尝试测试您的代码。变量  controlControlRequestPropertiesOrderProperty由您自己定义。我提供了一个关于XmlArryItemAttribute的类似示例供您参考。 

For your question, i try to test your code. The variable controlControlRequestPropertiesOrderProperty is defined by yourself. I make a similar example about XmlArryItemAttribute for your reference. 

public class Group
    {
        [XmlArrayItem(typeof(controlControlRequestPropertiesOrderProperty))]
        public controlControlRequestPropertiesOrderProperty[] orderProperties;

        /* Use the XmlArrayItemAttribute to specify types allowed
           in an array of Object items. */
    }



    public class controlControlRequestPropertiesOrderProperty
    {
        public int Number;

    }

    class _0130
    {
        static void Main(string[] args)
        {
            SerializeObject("TypeDoc.xml");
            DeserializeObject("TypeDoc.xml");
            Console.ReadKey();
        }
        public static void SerializeObject(string filename)
        {
            // Creates a new XmlSerializer.
            XmlSerializer s = new XmlSerializer(typeof(Group));

            // Writing the XML file to disk requires a TextWriter.
            TextWriter writer = new StreamWriter(filename);
            Group group = new Group();

            controlControlRequestPropertiesOrderProperty emp1 = new controlControlRequestPropertiesOrderProperty();
            controlControlRequestPropertiesOrderProperty emp2 = new controlControlRequestPropertiesOrderProperty();
            controlControlRequestPropertiesOrderProperty emp3 = new controlControlRequestPropertiesOrderProperty();

            emp1.Number = 3;
            emp2.Number = 7;
            emp3.Number = 10;

            controlControlRequestPropertiesOrderProperty[] emps = new controlControlRequestPropertiesOrderProperty[3] { emp1, emp2, emp3 };
            group.orderProperties = emps;

            // Creates an int and a string and assigns to ExtraInfo.
            // group.ExtraInfo = new Object[2] { 43, "Extra" };

            // Serializes the object, and closes the StreamWriter.
            s.Serialize(writer, group);
            writer.Close();
        }

        public static void DeserializeObject(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open);
            XmlSerializer x = new XmlSerializer(typeof(Group));
            Group g = (Group)x.Deserialize(fs);
            Console.WriteLine("Members:");

            foreach (controlControlRequestPropertiesOrderProperty e in g.orderProperties)
            {
                Console.WriteLine("\t" + e.Number);
            }
        }






最好的问候,

Wendy


这篇关于XmlSerializer - XmlWriter - 带对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆