0

So here is the code that throws the exception

    #region Header
            if (textBox2.Text != "")
            {
                try
                {
                    xmlTW.WriteStartElement("Header");
                    xmlTW.WriteRaw(Environment.NewLine);
                    xmlTW.WriteString(textBox2.Text);
                    xmlTW.WriteRaw(Environment.NewLine);
                    xmlTW.WriteEndElement();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            #endregion


            #region Body
            if (textBox3.Text != "")
            {
                try
                {
                    xmlTW.WriteStartElement("Rectangles");
                    xmlTW.WriteRaw(Environment.NewLine + textBox3.Text + Environment.NewLine);
                    xmlTW.WriteEndElement();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            #endregion

So, the problem is that the code will run smoothly only if one of the two textboxes is filled with data, if both of them contain text i get an invalid operation exception at WriteEndDocument();

I know it must be something simple, but i just can't figure it out :P . Any help is appreciated.

Thanks in advance.

2
  • Do you have this at the beginning of the document? <?xml version="1.0" encoding="utf-8"?> Commented May 27, 2013 at 18:58
  • Yeah i did, thanks for your reply anyway :P Commented May 27, 2013 at 19:12

2 Answers 2

4

Well-formed XML must have a Single root element. Following is well-formed XML:

<Header>
Some text
</Header>

It is well-formed because Header is root element there. However, following is not well-formed:

<Header>
Some text
</Header>
<Rectangles>
Some other text
</Rectangles>

To correct it, you got to put it in some root element.

<myRoot>
<Header>
Some text
</Header>
<Rectangles>
Some other text
</Rectangles>
</myRoot>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, didn't know i had to wrap everything in a generic container.
3

You need to set the XmlWriterSettings ConformanceLevel to Fragment.

This will enable you keep the xml struct that you wanted without adding root element

var xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.ConformanceLevel = ConformanceLevel.Fragment;

and create you xml write like this:

XmlWriter xmlTW = XmlWriter.Create("myFile.xml", xmlWriterSettings)

This link can help you start working with XmlWriter

2 Comments

Oh i see , this is very helpful, both answers are accepted, but i already solved it using the above solution.Will keep in mind your solution as well,also thanks for the tutorial.I will upvote once i reach the rep required. Thanks again.
This may probably resolve exception. However, I think its better practice to keep your XML well-formed.

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.