我一直在研究如何将 C# 类序列化为 XML 文件,但只找到了非常基本的示例。我正在尝试序列化一个类,该类也封装了其他类。
更具体地说,我有一个确定性有限自动机类,我在其中封装了一个转换列表、TotalStates 和 Alphabet。其中Transitions 和States 也被定义为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
}
这是一个过渡类
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();
}