1

I am trying to create the following XML document-

FileStream fs = new FileStream(path, FileMode.Create);

            XmlWriter w = XmlWriter.Create(fs);

            w.WriteStartDocument();
            w.WriteStartElement("People");
            w.WriteStartElement("Person");
            // loop over checked elements
            foreach (var xxx in checkedListBox1.Items)
            {

                w.WriteAttributeString("Name", textBox1.Text);
                w.WriteAttributeString("GamerTag", textBox2.Text);
                w.WriteAttributeString("Wins", textBox3.Text);
                w.WriteAttributeString("Losses", textBox4.Text);
                w.WriteAttributeString("Ratio", textBox5.Text);

                // get id of this match
                id = checkedListBox1.Text.Substring(1, 3);
                // call the function at the service to download the type of struct we require
                res = client.photo(id);
                format = System.Drawing.Imaging.ImageFormat.Jpeg;
                w.WriteElementString("Picture-id", res.ToString());
                if (checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex))
                {
                    // do something
                    w.WriteElementString("Game", checkedListBox1.Text);
                }
                w.WriteEndElement();
            }
            w.WriteEndElement();
            w.WriteEndDocument();
            w.Flush();
            fs.Close();

However on debugging I getback this error -

Token StartAttribute in state Element Content would result in an invalid XML document.

On the line - w.WriteAttributeString("Name", textBox1.Text);

I have checked for spaces etc however not sure why this is cropping up, any help is appreciated.

1
  • Employ using on your FileStream to ensure disposition. Commented Nov 28, 2011 at 16:18

3 Answers 3

3

The call to w.WriteStartElement("Person"); should be part of the loop.

After all, you call w.WriteEndElement() in the loop as well.

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

3 Comments

@leppie: Yeah, the comment already was an answer so I moved it. ;)
Seems the clown above you copied your comment. IIRC the text was identical. Makes me a sad panda.
@leppie: No, the text is not 100% identical. Apart from that, the error really is so obvious, it doesn't need copy/paste to reach the same conclusion. ;)
2

As Tomalak pointed out, the w.WriteStartElement("Person"); should be part of the loop. Apart from that, it is not nice to create the XML on the fly inside your GUI. You should consider building seperrate classes that hold your datastructure.

class Person 
{
    public string Name { get; set; }

    public string GamerTag { get; set; }

    // all other attributes go here
}

class People 
{
    public Person[] Persons { get; setM }
}

fill those classes either by hand or by binding them to the controls and generate your XML from thoses classes. And also consider changing the names of your controls. It is always nice to know what a control represents without finding it in a form first. textBox1is not really meaningful, NameTextBox is better choice, don't you think?

1 Comment

I see your point @esskar im not entirely sure I think the XML may be poorly structured due to the way I have written it out. Thanks for the input.
0

I think the line w.WriteStartElement("Person"); has to be written in the loop. You don't start an element in the loop, but you end it.

Comments

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.