473,387 Members | 1,504 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.

StringWriter and XmlTextWriter

Hi,

I'm having weird problems using StringWriter and XmlTextWriter. My code
looks like this:

StringWriter sw = new StringWriter(CultureInfo.InvariantInfo);
XmlTextWriter xtw = new XmlTextWriter(sw); // NullReferenceException
thrown here!
xtw.WriteStartDocument();
xtw.WriteStartElement("MyRoot");
/// Write some nodes.
xtw.WriteEndElement(); // MyRoot
xtw.WriteEndDocument();
xtw.Close();
string str = sw.ToString();

It's been working swell until now, and it still works from a test app I'm
using. However, when interacting with the real app we're developing, the
code now crashes at the second line - apparently XmlTextWriter's constructor
isn't too happy about the input its getting?

Hope someone can shed a bit of light on this - it's mighty dim at best to
me.

Note btw that everything works well if I omit the XmlTextWriter and write
everything to the StringWriter...

Kind regards,
Einar.

--
"If it was so, it might be; and if it were so, it would be; but as it isn't,
it ain't. That's logic" -- Lewis Carroll
Nov 15 '05 #1
4 9263
Einar Høst <_e*******@hotmail.com> wrote:
I'm having weird problems using StringWriter and XmlTextWriter. My code
looks like this:

StringWriter sw = new StringWriter(CultureInfo.InvariantInfo);
XmlTextWriter xtw = new XmlTextWriter(sw); // NullReferenceException
thrown here!
xtw.WriteStartDocument();
xtw.WriteStartElement("MyRoot");
/// Write some nodes.
xtw.WriteEndElement(); // MyRoot
xtw.WriteEndDocument();
xtw.Close();
string str = sw.ToString();

It's been working swell until now, and it still works from a test app I'm
using. However, when interacting with the real app we're developing, the
code now crashes at the second line - apparently XmlTextWriter's constructor
isn't too happy about the input its getting?

Hope someone can shed a bit of light on this - it's mighty dim at best to
me.

Note btw that everything works well if I omit the XmlTextWriter and write
everything to the StringWriter...


It seems very unlikely that it's really crashing there, although
obviously I wouldn't like to say for sure. If you put some other "new"
code in there (between the creation of the StringWriter and the
XmlTextWriter) can you step through that (and ensure that it does the
right thing)? It sounds like the kind of error which usually means the
debugger isn't debugging what it thinks it is.

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Einar Høst <_e*******@hotmail.com> wrote:
I'm having weird problems using StringWriter and XmlTextWriter. My code
looks like this:

StringWriter sw = new StringWriter(CultureInfo.InvariantInfo);
XmlTextWriter xtw = new XmlTextWriter(sw); // NullReferenceException
thrown here!
xtw.WriteStartDocument();
xtw.WriteStartElement("MyRoot");
/// Write some nodes.
xtw.WriteEndElement(); // MyRoot
xtw.WriteEndDocument();
xtw.Close();
string str = sw.ToString();

It's been working swell until now, and it still works from a test app I'm
using. However, when interacting with the real app we're developing, the
code now crashes at the second line - apparently XmlTextWriter's constructor isn't too happy about the input its getting?

Hope someone can shed a bit of light on this - it's mighty dim at best to
me.

Note btw that everything works well if I omit the XmlTextWriter and write
everything to the StringWriter...


It seems very unlikely that it's really crashing there, although
obviously I wouldn't like to say for sure. If you put some other "new"
code in there (between the creation of the StringWriter and the
XmlTextWriter) can you step through that (and ensure that it does the
right thing)? It sounds like the kind of error which usually means the
debugger isn't debugging what it thinks it is.

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

Hi Jon, thanks for replying.

I found it very unlikely too, so it took me a while to find "the bug". I
still don't understand why it fails. However, I did some very brute
testing - quite simply throwing an exception before and after the new
XmlTextWriter(sw) statement, like this:

First test:

StringWriter sw = new StringWriter(CultureInfo.InvariantInfo);
throw new MyAppException("Einar's first exception");
XmlTextWriter xtw = new XmlTextWriter(sw); // NullReferenceException
thrown here!

Second test:

StringWriter sw = new StringWriter(CultureInfo.InvariantInfo);
XmlTextWriter xtw = new XmlTextWriter(sw); // NullReferenceException
thrown here!
throw new MyAppException("Einar's second exception");

For the first test, I caught "Einar's first exception", for the second, I
got the NullReferenceException as before. I should mention also that I get
some further information: "Value cannot be null. Parameter name: s". I did
some searching on the web, and found this url which didn't exactly enlighten
me, but did seem to discribe problems with using XmlTextWriter:
http://www.dotnet247.com/247referenc...34/174127.aspx

I find it very strange that something would be able to corrupt my managed
memory...?

Regards,
Einar.
Nov 15 '05 #3
Einar Høst <_e*******@hotmail.com> wrote:
I found it very unlikely too, so it took me a while to find "the bug".
I still don't understand why it fails. However, I did some very brute
testing - quite simply throwing an exception before and after the new
XmlTextWriter(sw) statement, like this:


<snip>

That's very odd. As you say you've been unable to reproduce it in a
small test case, it's very hard to get much further :(

Hmm... hopefully someone else will be able to shed some light on it,
because I'm stumped...

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Einar Høst <_e*******@hotmail.com> wrote:
I found it very unlikely too, so it took me a while to find "the bug".
I still don't understand why it fails. However, I did some very brute
testing - quite simply throwing an exception before and after the new
XmlTextWriter(sw) statement, like this:


<snip>

That's very odd. As you say you've been unable to reproduce it in a
small test case, it's very hard to get much further :(

Hmm... hopefully someone else will be able to shed some light on it,
because I'm stumped...

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

Hi again Jon,

It's very hard indeed. And it gets weirder by the minute... currently, I'm
using code like this:

public string GetXml()
{
try
{
StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw); // That troublesome
line...
return sw.ToString();
}
catch (Exception ex)
{ // Catches nothing!!
throw new MyAppException("Rethrown exception", ex);
}
return "dummy";
}

From another assembly, the method is called like this:

try
{
string xml = xmlStore.GetXml();
}
catch (Exception ex)
{ // It ends up here...
MessageBox.Show("Message: " + ex.Message + "\r\nStack Trace: " +
ex.StackTrace);
}

Executed like this, I get a NullReferenceException, apparently stemming from
GetXml, according to the stack trace. However, for some reason it is NOT
rethrown as a MyAppException. Now if I remove the XmlTextWriter line, I get
no exception at all...

Regards,
Einar

Nov 15 '05 #5

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

Similar topics

5
by: Jain, Pranay Kumar | last post by:
Hello Everyone, I have written a simple app. that converts the dataset into excelspreadsheet. The App. uses the following architecture. First it generates the dataset with corresponding...
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
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...
3
by: Robert Linder | last post by:
I am trying to write 'StringWriter SW = new StringWriter();' to a FileStream. The code writes the text to file, but NotePad does not see the NewLines. Below is the code snippet taken from a...
4
by: quest | last post by:
Is there anyway I can generate the xml in the following format using XmlTextWriter ? Intended output: <?xml version="1.0" ?> I tried: XmlTextWriter xmlWriter = new...
4
by: fabrice | last post by:
Re hello, I m' sorry for my questions .. Under framework 1.1 with vb.net, i m using a StringWriter object to export in .xls file. To empty memory, I would like to use the propterty dispose on...
3
by: darrel | last post by:
I'm creating an RSS feed based off of this tutorial: http://www.uberasp.net/ArticlePrint.aspx?id=17 In a nutshell, they are doing: Response.Clear() Response.ContentType = "text/xml"...
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......
2
by: Harry Haller | last post by:
Can I remove text from a StringWriter ? I have a StringWriter object, the content of which looks like this: "<div>Some other html which I wish to keep</div>" Can I remove the unwanted <div>,...
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: 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: 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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.