Multiple XML Elements

M

mauricecosgrave

Hi all,

I need to create an XML file that has multiple instances of elements,
such as

<play>
<name></name>
<form></form>
<set></set>
</play>

<play>
<name></name>
<form></form>
<set></set>
</play>

The idea is that the user can create multiple plays in the xml file.
What's the best way to do this? I create one instance of the elements
with the following code:

play = xmldoc.CreateElement("play");
global.AppendChild(play);

name = xmldoc.CreateElement("name");
xmltext = xmldoc.CreateTextNode(currentPlayNameTextBox.Text);
name.AppendChild(xmltext);
play.AppendChild(name);

form = xmldoc.CreateElement("form");
xmltext =
xmldoc.CreateTextNode(formSelComboBox.SelectedItem.ToString());
form.AppendChild(xmltext);
play.AppendChild(form);

set = xmldoc.CreateElement("set");
mltext = xmldoc.CreateTextNode(setSelComboBox.SelectedItem.ToString());
set.AppendChild(xmltext);
play.AppendChild(set);

but if I try to just repeat this I get an error saying "This document
already has an 'XmlDeclaration' node.".

Any help would be much appreciated. Thanks.

Maurice
 
T

Tomek Kmiecik

Hello,

It is not possible. A valid XML document can have only one root element. But
you can create document like:

<plays>
<play>
<name></name>
<form></form>
<set></set>
</play>

<play>
<name></name>
<form></form>
<set></set>
</play>
</plays>

Tomek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top