> 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
Jon Skeet [C# MVP] - 26 Jul 2007 13:53 GMT
<snip>
> So I'm still convinced it's the ' in the beginning of e.Message that's causing it.
Can you produce a short but complete program which demonstrates the
problem?
See http://pobox.com/~skeet/csharp/complete.html for more details
about what I mean.
Jon
Gustaf - 26 Jul 2007 15:05 GMT
> 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
Jon Skeet [C# MVP] - 26 Jul 2007 16:24 GMT
> > Can you produce a short but complete program which demonstrates the
> > problem?
>
> 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
Gustaf - 27 Jul 2007 09:46 GMT
> 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
Jon Skeet [C# MVP] - 27 Jul 2007 09:55 GMT
> > 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?
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
Göran Andersson - 27 Jul 2007 19:19 GMT
>> 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?
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.

Signature
Göran Andersson
_____
http://www.guffa.com
Tom Porterfield - 26 Jul 2007 16:47 GMT
>> Can you produce a short but complete program which demonstrates the
>> problem?
>
> 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.

Signature
Tom Porterfield
Tom Porterfield - 26 Jul 2007 13:56 GMT
> 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:
[quoted text clipped - 14 lines]
> 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.

Signature
Tom Porterfield
Göran Andersson - 26 Jul 2007 16:11 GMT
>> What is the value of e.SourceUri, what is the value of e.Message?
>
[quoted text clipped - 6 lines]
> 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:
[quoted text clipped - 5 lines]
> 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.

Signature
Göran Andersson
_____
http://www.guffa.com