By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,334 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,334 IT Pros & Developers. It's quick & easy.

XML Error help

P: n/a
Hello,

I am fairly new to C# so bear with me if this is a simple question... I
couldn't find much help searching for it on the web.
Ok so I am trying to generate an XML document using the XmlTextWriter
class. Here's the pseudo/code :
XmlTextWriter writer = new XmlTextWriter(stream, Encoding.ASCII);

writer.WriteStartDocument(true);
bool doOnce = true;

foreach(String item in myArray)
{
if (doOnce == true)
{
writer.WriteStartElement("root", "");
writer.WriteAttributeString("rootattribute", "1");
doOnce = false;
}

writer.WriteStartElement("node", "");

writer.WriteElementString("name", "vishal");
if (checkBox.Checked)
writer.WriteAttributeString("type", "admin");
else
writer.WriteAttributeString("type", "guest");

if (checkBox2.Checked)
{
writer.WriteElementString("country", countryText.Text);
}

... // more if statements of the same kind.

writer.WriteEndElement();

}
writer.WriteEndElement();
writer.WriteEndDocument();

XmlDocument xd = new XmlDocument();
xd.Load(saveStream);

writer.Close();
stream.Close();
}
(Sincere apologies if this code is hard to read due to formatting)
This code compiles fine but the compiler points to an unhandled
exception of type "System.InvalidOperationException" occurring at the
line:
if (checkBox2.Checked)
and the additional information given is: "Token StartAttribute in state
Content would result in an invalid XML document"

I don't see any problems with the way the XMLElements are being formed.
Maybe you can make me see what's wrong. Any help will be greatly
appreciated!
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Vishal Bhavsar <vi********@yahoo.com> wrote:
I am fairly new to C# so bear with me if this is a simple question... I
couldn't find much help searching for it on the web.
Ok so I am trying to generate an XML document using the XmlTextWriter
class. Here's the pseudo/code :
<snip>
(Sincere apologies if this code is hard to read due to formatting)


In future, it would be appreciated if rather than apologising for it,
you'd fix it - if you fix it once, it saves several other people *each*
fixing it. It would also have been helpful to make it a *complete*
example that demonstrated the problem - see
http://www.pobox.com/~skeet/csharp/complete.html

Anyway, the problem itself is that you can't write an attribute in the
middle of thin air - you have to write it directly after starting an
element. Essentially, by the time you've done:

writer.WriteStartElement("node", "");
writer.WriteElementString("name", "vishal");

you've output:

<node>
<name>vishal</name>

so where would the attribute go?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.