StringWriter and XmlTextWriter

E

Einar Høst

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.
 
J

Jon Skeet [C# MVP]

Einar Høst said:
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.
 
E

Einar Høst

Einar Høst said:
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 - <[email protected]>
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/247reference/msgs/34/174127.aspx

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

Regards,
Einar.
 
J

Jon Skeet [C# MVP]

Einar Høst said:
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...
 
E

Einar Høst

Einar Høst said:
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 - <[email protected]>
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
 

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

Top