【问题标题】:How to create an XML file of an object如何创建对象的 XML 文件
【发布时间】:2019-05-24 16:58:34
【问题描述】:

我正在研究自动机中的单词识别,为了交换信息,我需要为我的对象创建一个 XML 文件。

已经使用 System.Xml.Serialization 的库;

[Serializable]
    class Automata
    {
        public List<int> estado_Q { get; set; }
        public List<char> alfabeto_X { get; set; }
        public List<Transicion> ftrans_t { get; set; }
        public int estadoInicio_qo { get; set; }
        public List<int> estadosFinales_F { get; set; }

        public Automata(List<int> Q, List<char> X, List<Transicion> T, int qo, List<int> F)
        {
            estado_Q = Q;
            alfabeto_X = X;
            ftrans_t = T;
            estadoInicio_qo = qo;
            estadosFinales_F = F;

        }

    }`
[Serializable]
    class Transicion
    {
        public int fromEstado { get; set; }
        public char leeyendo { get; set; }
        public int untilEstado { get; set; }


        public Transicion(int iEstado, char leer, int fEstado)
        {
            fromEstado = iEstado;
            leeyendo = leer;
            untilEstado = fEstado;
        }
    }
static void Main(string[] args)
        {
                     Clases.Automata au = new Clases.Automata();

            var automatalista = new Automata();
            XmlSerializer serializer = new XmlSerializer(typeof(List<Automata>));

            using (TextWriter writer = new StreamWriter("C:\\Users\\user\\Downloads\\Temp\\File.xml"))
            {
                serializer.Serialize(writer, automatalista);
            }

        }

我希望你生成一个 XML 文件,但我得到一个错误。`

System.InvalidOperationException: 'Create Xml Object.Program no es accesible por el nivel de protección. Sólo se pueden procesar tipos públicos.'

【问题讨论】:

    标签: c# xmlserializer


    【解决方案1】:

    您需要公开这些类型。 EG

    public class Automata . . .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 2018-07-29
      • 1970-01-01
      相关资源
      最近更新 更多