Ebikeneser
Ebikeneser

Reputation: 2364

Invalid XML when using XmlWriter in C#

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.

Upvotes: 1

Views: 5349

Answers (3)

esskar
esskar

Reputation: 10940

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?

Upvotes: 2

Fischermaen
Fischermaen

Reputation: 12458

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.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338208

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

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

Upvotes: 3

Related Questions