473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

XML Error help

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
1 1881
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Morten Gulbrandsen | last post by:
C:\mysql\bin>mysql -u elmasri -pnavathe company Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 29 to server version: 4.1.0-alpha-max-debug Type...
2
by: Maurizio Amoroso | last post by:
.... DTSRun: Executing... DTSRun OnStart: DTSStep_DTSExecuteSQLTask_1 DTSRun OnStart: DTSStep_DTSExecuteSQLTask_2 DTSRun OnStart: DTSStep_DTSExecuteSQLTask_4 DTSRun OnStart:...
6
by: Rowan | last post by:
Hello, I have been trying to solve this problem for a couple of weeks now with no progress. I created a package that works when it runs from my computer but when it runs as a scheduled job, it...
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
2
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton...
10
by: DataBard007 | last post by:
Hello Access Gurus: I use Win98SE and Access97. I just built a simple Access97 application which holds all contact information for my personal contacts, such as first name, last name, address,...
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
1
by: kvarada | last post by:
Hello Experts, I am building my application on WinNT.4.0_i386_MSVC.7.1 platform. When I build the application on a stand alone machine, it builds fine. But when I build the same application from a...
0
by: andy3381 | last post by:
Hi, I am having a scheduled job running DTS packages on my production environment. My production server server is having Window 2003 Sp2 installed on it. Before rebuilding the server I was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.