复杂对象的XML序列化

时间:2013-11-13 21:27:13

标签: c# xml-serialization

我一直在研究如何将C#类序列化为XML文件,但只找到了非常基本的例子。我正在尝试序列化一个类,其中也包含其他类。

更具体地说,我有一个确定性有限自动机类,其中我封装了一个Transitions,TotalStates和Alphabet列表。转换和状态也定义为C#类。

在尝试按原样序列化类之后,输出XML文件是空白的,所以我猜我需要做一些额外的工作才能使封装的类也能正常工作。我还需要将所有封装的用户定义类放在XML中。

这是我尝试序列化的类。现在我的属性没有初始化。

  public class XMLDoc
    {
        public XMLDoc()
        {
            AllStates = new List<State>();
            alphabet = new List<char>();
            transitions = new List<Transition>();

            alphabet.Add('1');
            alphabet.Add('0');


        }
        List<State> AllStates; // Our set of all States
        List<Char> alphabet;    // Our alphabet set
        List<Transition> transitions; //; Our set of transitions
        State startState, finalState;
    }

这是一个州级

  public class State
    {
        public State(int value)
        {
            id = value;
            type = StateType.Normal;
        }

        public void makeInitial()
        {
            type = StateType.Initial;
        }

        public void makeFinal()
        {
            type = StateType.Final;
        }
        public int getID()
        {
            return this.id;
        }

        int id;
        StateType type;

        List<Transition> transitions; // This list is going to be all transitions that have this State as the Initial value
    }

这是一个Transition类

  public class Transition
    {
        public Transition(State initial, Char accept, State final)
        {
            this.init = initial;
            this.acceptor = accept;
            this.final = final;
        }

        public int getInit(){
            return init.getID();
        }

        public int getFinal()
        {
            return final.getID();
        }

        public Char getAcceptor()
        {
            return acceptor;
        }
        State init; // This is the state we are transitioning
        Char acceptor; // This is the accepting alphabet value
        State final; // This is the state init becomes after accepting the acceptor
    }

最后,我希望XML的形式与此类似

<?xml version='1.0' ?>

<!-- XML Definition of a DFA by Matt Hintzke -->
<DFA>
    <!-- This defines the all used states in the DFA -->
    <STATES-SET>
        <STATE>q0</STATE>
        <STATE>q1</STATE>
        <STATE>q2</STATE>
        <STATE>q3</STATE>
    </STATES-SET>

    <!-- This defines the alphabet -->
    <ALPHABET>
        <CHARACTER>1</CHARACTER>
        <CHARACTER>0</CHARACTER>
    </ALPHABET>

    <!-- This defines all transitions -->
    <TRANSITION-SET>

        <!-- A transition block represents all transitions that can be made from a single initial state -->
        <TRANSITION-BLOCK>
            <!-- Hence, this block defines all transitions from the INIT-STATE q0 to any other states -->
            <INIT-STATE>q0</INIT-STATE>
            <!-- A transition represents any ACCEPTOR character from the INIT-STATE to some FINAL state -->
            <TRANSITION>
                <ACCEPTOR>1</ACCEPTOR>
                <FINAL>q1</FINAL>
            </TRANSITION>

            <TRANSITION>
                <ACCEPTOR>0</ACCEPTOR>
                <FINAL>q2</FINAL>
            </TRANSITION>
        </TRANSITION-BLOCK>



        <TRANSITION-BLOCK>
            <INIT-STATE>q1</INIT-STATE>
            <TRANSITION>
                <ACCEPTOR>1</ACCEPTOR>
                <FINAL>q0</FINAL>
            </TRANSITION>

            <TRANSITION>
                <ACCEPTOR>0</ACCEPTOR>
                <FINAL>q3</FINAL>
            </TRANSITION>
        </TRANSITION-BLOCK>


        <TRANSITION-BLOCK>
            <INIT-STATE>q2</INIT-STATE>
            <TRANSITION>
                <ACCEPTOR>1</ACCEPTOR>
                <FINAL>q3</FINAL>
            </TRANSITION>

            <TRANSITION>
                <ACCEPTOR>0</ACCEPTOR>
                <FINAL>q0</FINAL>
            </TRANSITION>
        </TRANSITION-BLOCK>



        <TRANSITION-BLOCK>
            <INIT-STATE>q3</INIT-STATE>
            <TRANSITION>
                <ACCEPTOR>1</ACCEPTOR>
                <FINAL>q2</FINAL>
            </TRANSITION>

            <TRANSITION>
                <ACCEPTOR>0</ACCEPTOR>
                <FINAL>q1</FINAL>
            </TRANSITION>
        </TRANSITION-BLOCK>
    </TRANSITION-SET>


    <!-- This defines all starting states -->
    <STARTING-SET>
        <STATE>q0</STATE>
    </STARTING-SET>

    <!-- This defines all final states -->
    <FINAL-SET>
        <STATE>q3</STATE>
    </FINAL-SET>
</DFA>

最后,这是序列化的代码......

    public void Serialize()
    {
        XMLDoc mydoc = new XMLDoc();

        XmlSerializer ser = new XmlSerializer(typeof(XMLDoc));
        StreamWriter writer = new StreamWriter("test.xml");
        ser.Serialize(writer, mydoc);
        writer.Close();
    }

1 个答案:

答案 0 :(得分:0)

这些类不会序列化,因为您有私有字段,但您需要使用公共属性。

更新: 下面是一个在LinqPad中运行的示例,它基于您的类(我删除了与序列化无关的方法):

void Main()
{
     XMLDoc mydoc = new XMLDoc();

        XmlSerializer ser = new XmlSerializer(typeof(XMLDoc));
        StreamWriter writer = new StreamWriter(@"D:\test.xml");
        ser.Serialize(writer, mydoc);
        writer.Close();
}

public class XMLDoc
    {
        public XMLDoc()
        {
            AllStates = new List<State>();
            alphabet = new List<char>();
            transitions = new List<Transition>();
            alphabet.Add('1');
            alphabet.Add('0');
        }
        public List<State> AllStates{get;set;} // Our set of all States
        public List<Char> alphabet{get;set;}    // Our alphabet set
        public List<Transition> transitions{get;set;} //; Our set of transitions
        public State startState{get;set;}
        public State finalState{get;set;}
    }


     public class State
     {
      //note - no explicitly defined default ctor here
      //that is OK as long as you don't have any other ctors defined.
      //although bear in mind that you probably should have one
      //to intialise 'type' and 'transitions'
      public int id{get;set;}
      public StateType type{get;set;}
      public List<Transition> transitions{get;set;} // This list is going to be all transitions that have this State as the Initial value
     }


    public class StateType
    {
      //did not have any code for this class...
    }

    public class Transition
    {
        public Transition(){} //must have default ctor to serialise.
        public Transition(State initial, Char accept, State final)
        {
            this.init = initial;
            this.acceptor = accept;
            this.final = final;
        }        
        public State init{get;set;} // This is the state we are transitioning
        public Char acceptor{get;set;} // This is the accepting alphabet value
        public State final{get;set;} // This is the state init becomes after accepting the acceptor
    }

这会产生:

<?xml version="1.0" encoding="utf-8"?>
<XMLDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AllStates />
  <alphabet>
    <char>49</char>
    <char>48</char>
  </alphabet>
  <transitions />
</XMLDoc>

请注意,您应该检查可用于控制生成的XML的“形状”的各种Serialisation Attributes,但说实话,我并非100%确定您可以使用所需的格式默认的XMLSerialiser。

您可以考虑查看可以从System.Xml.Linq(Linq到XML)使用的XDocument类来轻松地构建XML文档 - 它可能更适合您的格式类型想。

希望有所帮助。