使用具有变量名称的项目反序列化数组

时间:2015-01-28 15:16:59

标签: c# xml deserialization

我需要对以下XML示例进行反序列化,并且在整天尝试后我没有成功。

问题是元素名称不同,即使它们引用相同的原型。

最好的方法是什么?

<Test Name="SomeName">
    <OBJECTS NbObjects="2">
        <OBJECT0 ObjectName="ObjName1">
            <COMMAND NbCommand="3">
                <COMMAND0 CommandLabel="Command_A" CommandFileName="File1"/>
                <COMMAND1 CommandLabel="Command_B" CommandFileName="File2"/>
                <COMMAND2 CommandLabel="Command_C" CommandFileName="File3"/>
            </COMMAND>
        </OBJECT0>
        <OBJECT1 ObjectName="ObjName2">
            <COMMAND NbCommand="8">
                <COMMAND0 CommandLabel="MyCommand1" CommandFileName="File1"/>
                <COMMAND1 CommandLabel="MyCommand2" CommandFileName="File2"/>
                <COMMAND2 CommandLabel="MyCommand3" CommandFileName="File3"/>
                <COMMAND3 CommandLabel="MyCommand4" CommandFileName="File4"/>
                <COMMAND4 CommandLabel="MyCommand5" CommandFileName="File5"/>
                <COMMAND5 CommandLabel="MyCommand6" CommandFileName="File6"/>
                <COMMAND6 CommandLabel="MyCommand7" CommandFileName="File7"/>
                <COMMAND7 CommandLabel="MyCommand8" CommandFileName="File8"/>
            </COMMAND>
        </OBJECT1>
    </OBJECTS>
</Test>

这是我尝试的c#代码,没有运气:

using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace XMLtest
{
    [XmlRoot("Test")]
    public class Test
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlElement("OBJECTS")]
        public Objects objects { get; set; }

        public void Load(string filename)
        {
            XmlSerializer xs = new XmlSerializer(typeof (Test));
            using (StreamReader rd = new StreamReader(filename))
            {
                Test test = xs.Deserialize(rd) as Test;
            }
        }
    }

    public class Objects
    {
        [XmlAttribute("NbObjects")]
        public int NbObjects { get; set; }
        [XmlArray]
        [XmlArrayItem(typeof(Object))]
        public List<Object> ObjectList { get; set; }

        public Objects()
        {
            ObjectList = new List<Object>();
        }
    }

    public class Object
    {
        [XmlAttribute("ObjectName")]
        public string ObjectName { get; set; }

        [XmlElement("COMMAND")]
        public Commands Cmds { get; set; }
    }

    public class Commands
    {
        [XmlAttribute("NbCommand")]
        public int NbCommand { get; set; }

        [XmlArray("COMMAND")]
        [XmlArrayItem("COMMAND")]
        public List<Command> CommandList { get; set; }

        public Commands()
        {
            CommandList = new List<Command>();
        }
    }

    public class Command
    {
        [XmlAttribute("CommandLabel")]
        public string CommandLabel { get; set; }

        [XmlAttribute("CommandFileName")]
        public string CommandFileName { get; set; }
    }
}

1 个答案:

答案 0 :(得分:2)

我的建议是不要使用<OBJECT0 ... ><OBJECT1 ... ><COMMAND0 ... ><COMMAND1 ... >等。这些是不同的标签,意思是不同的物品/物品。

如果数字很重要,您可以将项目更改为<OBJECT id='0' name='...'> ... </OBJECT><COMMAND id='0' label='...' filename='...' > ... </COMMAND>。这将使您能够通过id对它们进行排序或识别,并且所有标签都属于同一类型。