InvalidOperationException

InvalidOperationException

我在一个项目中学习了C_'sXML,但是我一直得到InvalidOperationException。我把代码放在下面了

        XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.UTF8);
        writer.WriteStartDocument(true);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 4;

        writer.WriteStartElement("User Info");
            writer.WriteStartElement("Name");
                writer.WriteString(userName);
            writer.WriteEndElement();
            writer.WriteStartElement("Tutor Name");
                writer.WriteString(tutorName);
            writer.WriteEndElement();
        writer.WriteEndElement();

        writer.WriteStartElement("Course Data"); /*This is where the exception points to*/
            foreach (UserCourse c in courses)
            {
                String cn = c.Name;
                writer.WriteStartElement(cn);

                foreach (UserUnit u in c.Units)
                {
                    writer.WriteStartElement(u.Name.ToString());

                    foreach (UserObjective o in u.Objectives)
                    {
                        writer.WriteStartElement(o.Name.ToString());
                        writer.WriteString(o.Score.ToString());
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }
        writer.WriteEndElement();

        writer.WriteEndDocument();
        writer.Close();

也许有人能看出我做错了什么。谢谢你的帮助!

最佳答案

是“用户信息”和“课程数据”是不同的实体。如果是这样,我想您可以将它们封装在一个实体中。

            XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 4;

            writer.WriteStartElement("My Entity"); /* It is a biggest one*/
            writer.WriteStartElement("User Info");
            writer.WriteStartElement("Name");
            writer.WriteString(userName);
            writer.WriteEndElement();
            writer.WriteStartElement("Tutor Name");
            writer.WriteString(tutorName);
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteStartElement("Course Data"); /*This is where the exception points to*/
            foreach (UserCourse c in courses)
            {
                String cn = c.Name;
                writer.WriteStartElement(cn);

                foreach (UserUnit u in c.Units)
                {
                    writer.WriteStartElement(u.Name.ToString());

                    foreach (UserObjective o in u.Objectives)
                    {
                        writer.WriteStartElement(o.Name.ToString());
                        writer.WriteString(o.Score.ToString());
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteEndDocument();
            writer.Close();

关于c# - C#XML:System.InvalidOperationException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30972932/

10-10 02:34