如何正确定义类以将它们序列化为XML?

时间:2013-04-13 17:15:11

标签: c# xml xml-serialization xmlserializer xml-deserialization

我正在使用Nondeterministic有限自动机构建一个应用程序,它必须保存它并从文件中读取它。我用[XmlTags]尝试了几十种不同的方法,它仍然给我错误。你能告诉我如何定义类以通过XML序列化程序序列化吗?

 public class state
    {
        [XmlAttribute]
        public string name
        { get; set; }

        public List<transition> transitions
        { get; set; }

        public state()
        {
        }
        public state(string Name)
        {
            name = Name;
        }

        public void addTransition(string Letter, state NextState)
        {
            bool found = false;
            foreach (transition tr in this.transitions)
            {
                if (tr.letter.Equals(Letter))
                {
                    tr.nextStates.Add(NextState);
                    found = true;
                    break;
                }
                if (found == false)
                {
                    transitions.Add(new transition(Letter, NextState));
                }
            }
            if (found == false)
            {
                transitions.Add(new transition(Letter, NextState));
            }
        }
    }
    public class transition
    {
        [XmlAttribute]
        public string letter
        { get; set; }
        public List<state> nextStates
        { get; set; }

        public transition()
        {
        }
        public transition(string Letter, state NextState)
        {
            letter = Letter;
            nextStates = new List<state>();
            nextStates.Add(NextState);
        }
    }

    public class automaton
    {
        public List<state> states
        { get; set; }

        public automaton()
        {
        }
        public bool isState(string StateName)
        {
            foreach (state st in states)
                if (st.name.Equals(StateName)) return true;
            return false;

        }
        public state getState(string StateName)
        {
            foreach (state st in states)            
                if (st.name.Equals(StateName)) return st;
            return new state(StateName);
        }
    }  


public class XMLReader
    {
        public static void NFAToXML(automaton Automaton, string Filename)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(automaton));
            TextWriter textWriter = new StreamWriter(@Filename);
            serializer.Serialize(textWriter, Automaton);
            textWriter.Close();
        }
        public static automaton XMLToNFA(string Filename)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(List<state>));
            TextReader textReader = new StreamReader(Filename);
            automaton automaton = new automaton();
            automaton.states= (List<state>)deserializer.Deserialize(textReader);
            textReader.Close();
            return automaton;
        }

    }

我希望我的XML看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
<automaton xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <state name="q0" type="initial" >
        <transition letter="0">
          <state name="q1"></state>
          <state name="q3"></state>
        </transition>
        <transition letter="1">
          <state name="q0"></state>
          <state name="q2"></state>
        </transition>
    </state>
    <state name="q1">
        <transition letter="0">
        </transition>
        <transition letter="1">
          <state name="q3"></state>
        </transition>
    </state>
    <state name="q2" type="accepting">
        <transition letter="0">
          <state name="q1"></state>
        </transition>
        <transition letter="1">
          <state name="q0"></state>
          <state name="q2"></state>
        </transition>
    </state>
    <state name="q3">
        <transition letter="0">
          <state name="q1"></state>
        </transition>
        <transition letter="1">
          <state name="q1"></state>
          <state name="q2"></state>
        </transition>
    </state>
</automaton>

请帮助,我试图在很多方面改变它,但没有成功......

4 个答案:

答案 0 :(得分:2)

State有一个转换列表,每个转换都有一个状态列表。

这会在序列化时产生循环引用。如果你检查你捕获的异常的InnerException对象,它应该告诉你。

您需要解决此问题,例如只编写实例的ID,或使用支持引用的序列化程序。

答案 1 :(得分:1)

我能看到的主要错误(没有编译器)是列表:要做一个没有包装元素的列表,请使用XmlElementAttribute:

 [XmlElement("transition")]
 public List<transition> transitions { get; set; }

您也需要“状态” - [XmlElement("state")]

答案 2 :(得分:0)

您需要在类上定义[Serializable]属性以序列化/反序列化类对象。有关详细信息,请参阅链接here

[Serializable()]        
public class State
{ }

答案 3 :(得分:0)

如果你知道你想要的XML,有一个简单的伎俩:

  • 将文件另存为foo.xml
  • 在vs命令提示符下,执行xsd foo.xml以生成foo.xsd
  • 现在在同一财产中执行xsd foo.xsd /classes以生成foo.cs

foo.cs文件现在有一个与您的XML匹配的c#模型。