In a C# program in Visual Studio 2010 I have a large, multiline comment block starting with
/**
Part way through the comment block is a line that looks like this . . .
this.afIODisplay = "<" + lineToParse + Environment.NewLine +
this.afIODisplay;
At the point of the less-than symbol the color changes from comment-green to gray and the compiler issues a warning saying
XML comment on 'xxxxxxxxxxxxxxx' has badly formed XML -- 'A name was
started with an invalid character.'
(xxxx's replace proprietary code identifier) Why is the compiler trying to interpret the comment block as XML?
As an experiment I tried escaping the less-than symbol with
<
but that didn't fix it; it just replaced the warning with a new one saying
A name was started with an invalid character
and eliminated the transition to gray. I then replaced the starting line of the comment block with
/*
and the problem went away!! It also stayed gone with a whole line of asterisks. Why do the number of asterisks matter in C# comments? Or is this just a Visual Studio bug?
You have malformed XML in a documentation comment. This generates a warning according the specification. In Appendix A of the C# 5 spec:
Comments having a special form can be used to direct a tool to produce XML from those comments and the source code elements, which they precede. Such comments are single-line comments that start with three slashes (///), or delimited comments that start with a slash and two stars (/**).
and
The text within documentation comments must be well formed according to the rules of XML (http://www.w3.org/TR/REC-xml). If the XML is ill formed, a warning is generated and the documentation file will contain a comment saying that an error was encountered.
multiline comment in c# starts with /*
/** is used for XML comments
Related
I had to handle an exception, by catching it and matching the message and if the message contains a certain error code, do something (not relevant).
The exceptions message is this (in English, but the code and the gibberish after it is the same in any language):
$-5002 - $make sure that the consumed quantity of the component item would not cause the item's stock to fall below zero [ige1.whscode][line: 1] , 'production order no: 20580033 line: 1' [الرسالة 3559-7]
I had to work with the code 7-3559 (as displayed). In my code, I just did a e.Message.Contains("7-3559") and it failed to catch the exception. Wondering what went wrong I copy pasted the error massage to regex101.com and after a bit of trial and error I realized that e.Message.Contains("3559-7") is the real code and it works. I just don't know why. What messes up the string to display it in such a way that 7- is actually -7 and also behind 3559?
I guess I should also mention I am working with Visual Studio 2019 and C#.
Check out the regex here.
HxD:
This is a common issue encountered when using bidirectional text, in other words, a text that contains both texts directionality: Right-to-Left (RTL) such as Arabic texts, and Left-to-Right (LTR).
Here we have the Arabic text mixed with English text so some rules will be applied to the text to determine the directionality. You may find details about this here.
In short, the text you see in the debugger is how the text will appear when you print it but not how it is represented in memory.
Here I use Linqpad to paste the text and the editor has immediately transformed it into the representation in memory. And once printed, the text is shown with a different directionality.
The system I'm working on uses DataSet.ReadXml(XmlReader) to read an XML file and load its contents to a DataSet. The XML file is from a business partner and may not always be well-formed, but this system is expected to perform reasonable corrections to the input.
We've seen errors in the XML input files, such as:
Case 1: In the middle of a string value, use of characters such as
'<', '>', or my favorite, '&', which causes "An error occurred while parsing
EntityName. Line x, position y."
Case 2: In the middle of a string value, weird constructs such as
"<3" so that the text depicts a heart, which causes "Name cannot
begin with the '3' character. Line x, position y."
Case 3: Invalid characters for the given encoding, which causes
"Invalid character in the given encoding. Line x, position y."
If some simple rules are adopted, these errors can be addressed programmatically:
Case 1: Replace the offending character with its XML character entity
("&" becomes "&", etc.
Case 2: Replace the "<" in "<3" with a space, so that it becomes " 3"
Case 3: Replace the invalid character with a space
However, all of these errors raise the same exception: System.Xml.XmlException
I would like to take an appropriate action when any of these errors are encountered, but what's the best way to do that? These three different errors all have the same HRESULT value (-2146232000), and so far the only way I have been able to differentiate amongst them is by inspection of the XmlException.Message string property.
String comparison seems a lousy way to determine the exact cause of the error. Were I to follow that approach, the code would break should the exception message change in future versions of .NET. It would also not be portable to some languages.
Therefore, how does one programmatically differentiate between the various types of errors that could be represented in an XmlException?
EDIT
In the comments below I've received advice about the importance of ensuring that XML data is of high quality. I don't disagree, but as my question states, it's outside my control and I can do nothing about it. So, as well-intentioned as your remarks are, they miss the point. If you know a good way to differentiate amongst the very many errors that can be presented by the System.Xml.XmlException class, please, share your knowledge. Thank you.
Rather than trying to parse non-XML with an XML parser and catching the errors, if you really want to process non-XML then I would try preprocessing it with a parser for the particular non-XML grammar that you want to accept. Before you ever submit the data to an XML parser, run it through a Perl script or similar that recognizes the patterns that you want to convert to XML, then run the result through an XML parser.
I'm getting this unexpected character '' error and I don't understand why.
var list = new List<MyModel>();
list.Add(new MyModel() {
variable1 = 942,
variable2 = 2001,
variable3 = "my text",
variable4 = 123
}); // CS1056 Unexpected character '' on this line
From what the error says and the actual error code I got from an Online compiler after copy/pasting, Your code on this line contains a character that is not visible but that the compiler is trying to interpret. Simply try erase every character starting at your closing bracket towards your number 3 and press Enter again It should be working (it did work for me)
I just deleted the file Version=v4.0.AssemblyAttributes.cs(1,1,1,1) located in my temp folder C:\Users\MyUser\AppData\Local\Temp and then it works perfectly.
For .NET Core you have to delete .NETCoreApp,Version=v2.1.AssemblyAttributes.cs
As mentioned by Daneau in the accepted answer, the problem is by a character that is not visible in the IDE.
Here are several solutions to find the invisible character with Notepad++.
Solutions 1: Show Symbol
Copy the code to Notepad++,
Select View -> Show Symbol -> Show All Characters
This can show invisible control characters.
Solutions 2: Convert to ANSI
Copy the code to Notepad++,
Select Encoding- > Convert to ANSI
This will convert the invisible character to ? if it is a none ANSI character.
Solutions 3: Remove none ASCII characters
Copy the code to Notepad++,
Open the Find window (Ctrl+F)
Select the Replace tab
in "Find what" write: [^\x00-\x7F]
Leave "Replace with" empty
In "Search Mode" select "Regular expression"
Find and remove the none ASCII characters
This will remove none ASCII characters.
Note: This can remove valid non ASCII characters (in strings and comments) so try to skip those if you have any.
Tip: Use HEX-Editor plugin
Use Notepad++ HEX-Editor plugin to see the binary code of text. Any character out of the range of 0x00 - 0x7F (0 - 127) is a non ASCII character and a suspect of being the problem.
Just reporting my direct experience.
As Daneau wrote, I had a character (ASCII DLE, I copied while messing up a zebra printer) hiding in the text. I could not afford to rewrite everything, so I used notepad++ "View->Show Symbol->Show All Characters" feature.
I apologize for not commenting Daneau entry, but I don't have enough reputation.
Write the code again without copying it. That worked for me
go to C:\Users\UserName\AppData\Local\Temp\ and clear the data or remove the file specified in the error, that will solve the issue.
VS will add the required file on auto, no worries.
I got this error when I moved my application from one folder to another, I resolved this by deleting the Debug folder inside the obj folder.
It indeed has to do with copy pasting code and characters that you cannot see. The easiest way to fix it is by passing your copy pasted code into a note application or simple text program which will automatically remove these invisible characters. After that simply copy the code from the text editor and paste it into your IDE.
For some reason this happened to me on every project in my solution. My fix was to delete all bin and obj folders in my solution.
Does someone have a script or snippet for Visual Studio that automatically removes comment headers for functions or classes?
I want to remove comments like.
/// <summary>
///
/// </summary>
Actually anything that removes comments starting with /// would really help. We have projects with massive amount of GhostDoc comment that really just hides the code and we are not using the document output. It is a tedious work for us to remove theese comments.
Open Quick Replace (CTRL + H) in Visual Studio and replace :b+///.*\n with an empty string (ensure you set Use to Regular expressions). This should allow you to get rid of the comments in the specified scope.
You can use this Regex \/\/\/.*<summary>.*<\/summary>, with these options gms, to match the string. You can replace that with nothing. This can be done in Notepad++ or Visual Studio.
Here is a Regex 101 to prove it.
You can use Python (with regexps, if you wish):
#! /usr/bin/python
import sys
if len(sys.argv) < 2:
print( "Usage: removelines <source-file>" )
exit(0)
InFileName = sys.argv[1]
Out = open( InFileName + ".out", "w" );
for Line in open( InFileName ).readlines():
if Line.lstrip().find( "///" ) == 0:
print( "Skipping line", Line )
continue
Out.write( Line )
You could use find and replace in files in Notepad++ (or any other advanced text editor) using regex.
You can just do find and replace with an empty string in visual studio. You already have the strings you want to get rid of. You could also parse the code files as text and remove any line that starts with a comment character.
I'm new to C#, and want to develope a program with which I could delete the comments after // in my code. Is there any simple code recommended for this purpose?
It has been suggested that you just search for "//" and trim.
Because you have limited yourself to single-line commands this seems like a relatively simple exercise however it has some tricky cases you need to be thinking about if you intend for the output of the program to be a valid C# application with identical behavior to the input program.
Here are some examples where just searching for "//" and trimming won't work.
Comment in Literal:
string foo = "this is // not a comment";
Comment in Comment
/* you should not trim // this one */
Comment in Comment Part Deux
// This is a comment // so don't just remove this!
Multi-line Comment Adjacency
/* you should not *//* trim this these */
There are certainly other edge cases but these are some low-hanging fruit to think about.
First point, this seems like a bad idea. Comments are useful.
Taking it as an exercise,
Edit: This is a simple solution that will fail on all the case #Bubbafat mentions (and propbably some more). It would still work OK on most source files.
read the text one line at a time.
find the last occurrence of //, if any using String.LastIndexOf()
remove the text after (including) the '//' when found
write the line to the output
ad 1: You can open an TextReader using System.IO.File.OpenText(), or File.ReadLines() if you can use Fx4
Also open an output file using System.IO.File.WriteText()
ad 3: int pos = line.LastIndexOf("//"); if (pos >= 0) { line = line.Substring(0, pos); }