473,404 Members | 2,213 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,404 software developers and data experts.

String starting with ' won't concatenate

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
11 2388
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
3
by: Locia | last post by:
I have a Template base class and another subclasses of Template. I have a tree structure of template object. class Template { ArrayList child; public virtual String Eval(Environment env) {
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
6
by: Registered User | last post by:
Hi experts, I'm trying to write a program to solve the following exercise: Accept three strings from the user. Find the first occurrence of the first string in the third string. If it is present...
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
7
by: largedimples | last post by:
The assignment was as follows: A character string can be implemented as a linked list of characters. Implement a C++ ADT called Newstring that uses linked lists to implement the following string...
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
6
by: James Arnold | last post by:
Hello, I am new to C and I am trying to write a few small applications to get some hands-on practise! I am trying to write a random string generator, based on a masked input. For example, given...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.