473,387 Members | 1,745 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,387 software developers and data experts.

Problem with XmlTextWriter

Hi,

I am currently trying to output my environment variables to a file in
XML. It's currently failing with an InvalidOperationException when it
executes the xw.WriteStartElement("Variable") for the second time. The
exception details are: "Token StartElement in state Epilog would result
in an invalid XML document."

I'm sure it's something obvious. What am I missing?

Regards,
Tony

XmlTextWriter xw = new
XmlTextWriter(@"C:\test.xml",Encoding.UTF8);
xw.WriteStartDocument();
foreach (DictionaryEntry de in
Environment.GetEnvironmentVariables())
{
xw.WriteStartElement("Variable");
xw.WriteAttributeString("Key", de.Key.ToString());
xw.WriteAttributeString("Value", de.Value.ToString());
xw.WriteEndElement();
}
xw.WriteEndDocument();
xw.Flush();
xw.Close();

Sep 6 '06 #1
1 2583
oops, I forgot the root element. Sorry. I plead temporary insanity.

to***********@consultant.com wrote:
Hi,

I am currently trying to output my environment variables to a file in
XML. It's currently failing with an InvalidOperationException when it
executes the xw.WriteStartElement("Variable") for the second time. The
exception details are: "Token StartElement in state Epilog would result
in an invalid XML document."

I'm sure it's something obvious. What am I missing?

Regards,
Tony

XmlTextWriter xw = new
XmlTextWriter(@"C:\test.xml",Encoding.UTF8);
xw.WriteStartDocument();
foreach (DictionaryEntry de in
Environment.GetEnvironmentVariables())
{
xw.WriteStartElement("Variable");
xw.WriteAttributeString("Key", de.Key.ToString());
xw.WriteAttributeString("Value", de.Value.ToString());
xw.WriteEndElement();
}
xw.WriteEndDocument();
xw.Flush();
xw.Close();
Sep 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
3
by: Pete | last post by:
I'm trying to read an XML document and write out a slightly modified version using the XmlTextWriter. I'm basically trying to copy all the nodes exactly as they are read and do some text...
4
by: z. f. | last post by:
i have xml with the line <VAL ID="artist" VAL="abc & cde"/> i need the & character to be there but the xmlDocument Load method throw exception for invalid character. i don't want to encode...
3
by: Olav Tollefsen | last post by:
I'm using the following code to create an Xml document to be saved by the browser (IE): MemoryStream stream = new MemoryStream(); OrderManager.ExportToXml(stream); // Creates...
2
by: Steve | last post by:
I'm an XML newb. I'm serializing a class and when I inspect the xml file, all the data is on one line rather than being nested and indented Is that normal? <code> StreamWriter sw = new...
4
by: Einar Høst | last post by:
Hi, I'm having weird problems using StringWriter and XmlTextWriter. My code looks like this: StringWriter sw = new StringWriter(CultureInfo.InvariantInfo); XmlTextWriter xtw = new...
2
by: fmancina | last post by:
Hi, I am employing the XmlTextWriter class to generate an XML document. Everything works fine, until I have to write an attribute to an element which contains a value. Examples below: //...
3
by: lisa.bogart | last post by:
I am getting a "Root element is missing" error when I try to load a stream into and XmlDocument. Can anyone help me with what I am doing wrong??? Dim xmlTextWriter As XmlTextWriter = Nothing...
3
by: GaryDean | last post by:
I'm using an XmlTextWriter and it's various methods such as WriteElementString, WriteStartElement, WriteEndElement, etc to create an xml document. When I instantiate the XmlTextWriter to a file......
8
by: ajc308 | last post by:
I am using C# to transform an XML document which actually outputs another XML document using XslCompiledTransform and an XmlTextWriter. The code I am using is: XslCompiledTransform myXslTrans...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.