By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

String starting with ' won't concatenate

P: n/a
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
Jul 26 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Gustaf wrote:
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.
--
Tom Porterfield
Jul 26 '07 #2

P: n/a
Tom Porterfield wrote:
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
Jul 26 '07 #3

P: n/a
On Jul 26, 1:33 pm, Gustaf <gust...@algonet.sewrote:

<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

Jul 26 '07 #4

P: n/a
Gustaf wrote:
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.
--
Tom Porterfield
Jul 26 '07 #5

P: n/a
Jon Skeet [C# MVP] wrote:
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
Jul 26 '07 #6

P: n/a
Gustaf wrote:
Tom Porterfield wrote:
>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.
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.

--
Göran Andersson
_____
http://www.guffa.com
Jul 26 '07 #7

P: n/a
On Jul 26, 3:05 pm, Gustaf <gust...@algonet.sewrote:
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

Jul 26 '07 #8

P: n/a
Gustaf wrote:
Jon Skeet [C# MVP] wrote:
>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.
--
Tom Porterfield
Jul 26 '07 #9

P: n/a
Jon Skeet [C# MVP] wrote:
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
Jul 27 '07 #10

P: n/a
On Jul 27, 9:46 am, Gustaf <gust...@algonet.sewrote:
Jon Skeet [C# MVP] wrote:
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

Jul 27 '07 #11

P: n/a
Gustaf wrote:
Jon Skeet [C# MVP] wrote:
>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.

--
Göran Andersson
_____
http://www.guffa.com
Jul 27 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.