2

I have been learning C#'s XML with a project however I keep getting the InvalidOperationException. I have put the code below

        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();

Perhaps someone can see what I am doing wrong. I appreciate any help!

3
  • what are you trying to achieve here ? Commented Jun 22, 2015 at 6:01
  • Creating a user record that uses XML to save an external file. Where the course and its subsections are saved as well. Commented Jun 22, 2015 at 6:24
  • When I was learning C#'s XML first time, I spent so much time learning how XmlWriter and XmlReader works. It is great to understand this first, understand how it's actually working. However, I strongly advise you not to waste your time working with it. Use XML Serialization and classes. It lets you avoid over-coding. The whole code provided by you in a question could be replaced with a class definition and 3 rows of code. Read more here: msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx Commented Jun 22, 2015 at 6:30

2 Answers 2

3

XML Element names cannot contain spaces.

Refer to XML Naming Rules.

Also it seems like you should have one root element. Like CourseData should be inside UserInfo.

Checkout Fiddle

Sign up to request clarification or add additional context in comments.

6 Comments

Wow. I had no idea! It does not seem to have worked though
Really? Could you post what code you had so I can do a comparison?
@JeremyBeare click on Fiddle link to get more idea. Hope it will help.
I used the Fiddle link to check and compare. I have made all the changes and it still causes the Exception. If I remove the "CourseData" elemental and all within it, the code works fine.
Create a new Fiddle and share the link.
|
1

Is "User Info" and "Course Data" is a different entity. If it is so, I think you may encapsulate them in one entity.

            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();

1 Comment

They are both in the User Info entity. It is a very short entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.