String starting with ' won't concatenate

G

Gustaf

Some error handling code:

catch (System.Xml.XmlException e)
{
Console.WriteLine("Error in " + e.SourceUri + ": " + e.Message);
return;
}

The output:

' is an unexpected token. The expected token is '>'. Line 17, position 50.

Everything in the string before e.Message is deleted. I suspect that this is because the Message property starts with '. Is there a workaround for this?

Gustaf
 
T

Tom Porterfield

Gustaf said:
Some error handling code:

catch (System.Xml.XmlException e)
{
Console.WriteLine("Error in " + e.SourceUri + ": " + e.Message);
return;
}

The output:

' is an unexpected token. The expected token is '>'. Line 17, position 50.

Everything in the string before e.Message is deleted. I suspect that
this is because the Message property starts with '. Is there a
workaround for this?

What is the value of e.SourceUri, what is the value of e.Message? Your
suspicion is incorrect. Ex:

string uri = "http://www.uri.com/";
string message = "' is an unexpected token. The expected token is '>'.
Line 17, position 50.";
Console.WriteLine("Error in " + uri + ": " + message);

Outputs:

Error in http://www.uri.com/: ' is an unexpected token. The expected
token is '>'. Line 17, position 50.
 
G

Gustaf

Tom said:
What is the value of e.SourceUri, what is the value of e.Message?

The value of e.SourceUri is "file:///d:/test/po.xsd". The value of e.Message is the string starting with '. So the full output I expect is:

Error in file:///d:/test/po.xsd: ' is an unexpected token.
Line 17, position 50.

As soon as I use e.Message and it has this particular message (starting with '), it removes everything before the e.Message part.

If I add a string *after* e.Message, it stays there. Also, if I make it show another error (not starting with '), it works:

Error in file:///d:/test/po.xsd: The 'xs:element' start tag on
line 17 does not match the end tag of 'xs:element_'. Line 17,
position 52.

So I'm still convinced it's the ' in the beginning of e.Message that's causing it.

Gustaf
 
T

Tom Porterfield

Gustaf said:
The value of e.SourceUri is "file:///d:/test/po.xsd". The value of
e.Message is the string starting with '. So the full output I expect is:

Error in file:///d:/test/po.xsd: ' is an unexpected token.
Line 17, position 50.

As soon as I use e.Message and it has this particular message (starting
with '), it removes everything before the e.Message part.

If I add a string *after* e.Message, it stays there. Also, if I make it
show another error (not starting with '), it works:

Error in file:///d:/test/po.xsd: The 'xs:element' start tag on
line 17 does not match the end tag of 'xs:element_'. Line 17,
position 52.

So I'm still convinced it's the ' in the beginning of e.Message that's
causing it.

Can you post a short but complete example that demonstrates the problem?
I am unable to reproduce the situation with the following:

string uri = "file:///d:/test/po.xsd";
try
{
string message = "' is an unexpected token. The expected token is
'>'. Line 17, position 50.";
System.Xml.XmlException ex = new XmlException(message, null, 17, 50);
throw new System.Xml.XmlException(message);
}
catch (System.Xml.XmlException ex)
{
Console.WriteLine("Error in " + uri + ": " + ex.Message);
}

That shows the expected output.
 
G

Gustaf

Jon said:
Can you produce a short but complete program which demonstrates the
problem?

Not exactly short, but here goes...

1. Create a new console app, and paste in this code:

using System;
using System.Xml;
using System.Xml.Schema;

namespace XmlTest
{
class Program
{
static void Main(string[] args)
{
// Schema and XML file names
string uriOfSchema = @"d:\test\po.xsd";
string xmlToValidate = @"d:\test\po.xml";

// Define XmlReader settings
XmlReaderSettings settings = new XmlReaderSettings();
settings.CheckCharacters = true;
settings.CloseInput = true;

// Try reading schema
try
{
settings.Schemas.Add(null, uriOfSchema);
}
catch (System.Xml.XmlException e)
{
Console.WriteLine("Error in " + e.SourceUri + ": " + e.Message);
return;

/* NOTE: If e.Message starts with ', everything before e.Message is
* strangely removed. */
}

settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

try
{
using (XmlReader xmlReader = XmlReader.Create(xmlToValidate, settings))
{
if (xmlReader != null)
{
while (xmlReader.Read());
Console.WriteLine("Validation completed.");
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

private static void ValidationCallBack(object sender, ValidationEventArgs args)
{
switch (args.Severity)
{
case XmlSeverityType.Error:
Console.WriteLine("Error: " + args.Message);
break;
case XmlSeverityType.Warning:
Console.WriteLine("Warning: " + args.Message);
break;
}
}

}
}

2. Then make a file called po.xsd and paste in this code:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a" type="xs:string"/
</xs:schema>

(There is an intentional well-formedness error on line 3.)

3. Put this file in a folder of your choice and correct the variable 'uriOfSchema' in the code. You won't need the XML file to run this test.

Gustaf
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Gustaf said:
The value of e.SourceUri is "file:///d:/test/po.xsd". The value of
e.Message is the string starting with '. So the full output I expect is:

Error in file:///d:/test/po.xsd: ' is an unexpected token.
Line 17, position 50.

As soon as I use e.Message and it has this particular message (starting
with '), it removes everything before the e.Message part.

No, it doesn't. Just because you can't see that part on the screen when
the output is complete, doesn't mean that it's removed from the string,
and doesn't mean that it wasn't displayed om the screen.

The most likely explanation is that the message actually starts with a
carreage return character. That makes the cursor return to the beginning
of the line, and the message will overwrite the first part of the string.
If I add a string *after* e.Message, it stays there. Also, if I make it
show another error (not starting with '), it works:

Error in file:///d:/test/po.xsd: The 'xs:element' start tag on
line 17 does not match the end tag of 'xs:element_'. Line 17,
position 52.

So I'm still convinced it's the ' in the beginning of e.Message that's
causing it.

That conclusion is based on the assumption that you can actually see the
entire string when it has been written to the screen.
 
J

Jon Skeet [C# MVP]

Not exactly short, but here goes...

<snip>

The problem is that the second character of the message is a carriage
return.

Change your code to:

Console.WriteLine("Error in " + e.SourceUri + ": " +
e.Message.Replace("\r", "\\r"));

and you'll see what I mean.

Jon
 
T

Tom Porterfield

Gustaf said:
Not exactly short, but here goes...

As Jon says, you have a carriage return as the second character of
e.Message. An inspection of that in the debugger shows this.
 
G

Gustaf

Jon said:
The problem is that the second character of the message is a carriage
return.

Thank you both. So Message strings of .NET exception classes may contain whitespace other than SPACE? Does it mean it's generally good practice to normalize these strings before presenting them?

Gustaf
 
J

Jon Skeet [C# MVP]

Thank you both. So Message strings of .NET exception classes may
contain whitespace other than SPACE?

There's nothing to prevent it, certainly.
Does it mean it's generally good
practice to normalize these strings before presenting them?

It depends on how you're presenting them. This is a pretty rare
occurrence, to be honest - but it's not a bad idea to be robust about
your presentation.

Jon
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Gustaf said:
Thank you both. So Message strings of .NET exception classes may contain
whitespace other than SPACE?

As it was a carriage return that was the unexpected token, that's what's
put in the error message. In escaped form, the error message would start:

'\r' is an unexpected token.

As you have that character in the input to the method, it might end up
in the exception message.
Does it mean it's generally good practice
to normalize these strings before presenting them?

You might want to do that.

In certain situations, like if you output the message on a web page,
it's crucial for security that you don't display the message without
encoding it properly, otherwise it could be used for cross site
scripting (XSS) atacks.
 

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