C# differentiate exceptions [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In C# I need to catch an XMLException but I also have to differentiate it, because it can be either Xml_InvalidRootData or Xml_UnexpectedEOF.
How can I achieve this?
Those strings I can only see in debugger with an alias of "ResString".
But I want to have multi-culture solution, so string comparison is something I want to avoid as much as possible.
HResults are the same.

If you take a look at
https://referencesource.microsoft.com/#System.Runtime.Serialization/System/Xml/XmlExceptionHelper.cs
you'll see that throughout, there is a lot of work done to get a (possibly localized) error string, which is then the only argument to new XmlException.
As you correctly note, if you need to distinguish between different exception conditions to make some programmatic response, this is a whole lot of no help.
Since you do not want to examine the strings -- and that is a reasonable choice -- your best bet is probably to write your own XML parser that has the output you desire.
Consider the design of such a parser carefully. The output that you want is not the structured XML, but rather a detailed report explaining why it is not legal XML. Exceptions are a mechanism for handling exceptional situations; the designers of the XML parser considered malformed XML to be an exceptional situation; they thought this scenario should almost never happen. Since it almost never happens, and since when it does happen, there's nothing the program can do about it, there is no incentive to produce a detailed report that allows programmatic decisions to be made on the basis of what errors were detected.
But that is apparently not your situation; you have the opposite situation of the designers of the XML parser. You care about the error, and you wish to do something different depending on different errors, so the output of your parser should be the error report, not the XML syntax tree. It should not throw exceptions at all, because in your scenario, a malformed XML document is not exceptional; you expect it.
XML is not a particularly difficult language to lex and parse (provided you are not also trying to solve the problem of "is this document a valid instance of this schema?", which is a harder problem) so it should not take you long to produce an error-detecting lexer and parser, particularly since you have the source code of existing XML parsers to guide you. Good luck!

Related

Can someone please explain (nicely) why OOP Method Overloading is a good thing? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
First, please no spamming because I am not necessarily an OOP devotee. That said, I have been a programmer on and off for almost 30 years and have created a lot of pretty cool production code systems/solutions in several industries. I've also done my share of break/fix, database development, etc. Even a bout 10 years as a web programmer, not developer, so I an not so much a newbie but someone trying to get an answer about something that frankly is eluding me.
I started as a "C" programmer int he early 1980's and "C" served me well into the early 2000s (even today most scripting and higher level languages use "C" syntactical elements).
That said, overloading seems to violate every principle of what I was taught were "good coding practices" by increasing ambiguity in the opportunity for omission of intended code to be executed for a given condition or actually running a routine you didn't expect to due to some condition falling through the cracks. Also generally seems to creates LOTS of confusion for learners.
I am not saying overloading is bad per se, I just want to better understand it's practical application to real problems other than simply a way to provide input validation or perhaps just to handle inputs from other sources that you have no control over in an API or something else that you don't necessarily know the type of (again not clear on how or why that could actually happen either) C# has a lot of parse and try catch to handle this as do most OOP languages.
In over a decade, I have yet to get a straight, non judgmental and dare I say unsnarky answer to this question. Surely there is someone who can offer a reasonable explanation of why it is used.
So I pose the question to you the stack overflow gurus, Personally, does having a method/function that is potentially callable multiple different ways with multiple exclusive code segments really a good thing, or does it just suggest lack of good planning when designing software. Again, not knocking, judging, or disparaging, I just don't get it.....please enlighten me!
I'd say std::to_string is a pretty good example of good use of overloading. Why would you want to have different functions for converting different types to std::string? You don't. You just want one - std::to_string and you want it to behave sensibly whatever type of argument you give it - and it does just that. Using overloading keeps the client code simple.

What do I need to learn to write a lexer parser interpreter for ZPL? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
For those that haven't seen it, ZPL is a text-based markup language used for printing labels from special-purpose printers. A UPS shipping label would be a good example.
The markup language is quite primitive. There are modal commands to set a few state values, but the bulk of the language is one-liners that take the form <command><parameters><data>. There is no flow-control or nesting.
I want to write a rendering engine that interprets ZPL and renders to an image. There are a couple projects that have started down this road, but those projects are not complete enough for commercial use.
My question is: What do I need to learn to write a great <something> that will parse a ZPL document and convert that into commands I can execute? (By 'execute', I mean draw.)
Is this a parser? a lexer? tokenizer? interpreter? Is there some subset of compiler design theory that would be the most efficient path to success for this kind of project? ... and what is that called?
In your case, it is probably an interpreter.
If there are no conditionals, then presumably the markup is produced by reading the ZPL document from top to bottom.
What you need is a parser to tear apart the ZPL commands. If you are lucky after you tear one apart, everything you need to execute is available, either from the parameters of the command, or from previous ZPL modal commands.
So:
loop
parse ZPL fragment
execute ZPL fragment
end loop
If you want to read about building simple parsers, you can do it here: Is there an alternative for flex/bison that is usable on 8-bit embedded systems?
It should be obvious that at each point where the parse has recognized a construct, it has all the information from the content of the construct, thus you are prepared to execute it.
Of course, you have to know what the ZPL commands mean, and have the machinery available to execute them. Not everbody knows how to convert markup requests into updates on an image. If you know how to do that, you just a lot of sweat in front of you (why don't you just go get a working version of ZPL?). Best of luck to you.
If you don't know how to do that, you'll need to ask another question. And if you have to ask about all of parsing/interpreting/executing you're not likely to finish this project.
You need to understand what regular expressions are, the single rule that governs the lexer (longest match wins), and how the parser works.
There is good intro to this subject (and fun) by Scott Stanchfield ANTLR 3.x Tutorial -- worth watching even if you won't use ANTLR.
And then you would need to know ZPL itself.
For C# there are already some lexer and parser generators usually with some examples to make you comfortable -- just take your pick from Irony, Coco/R, GOLD, ANTLR, LLLPG, Sprache, or my NLT.

Which is better Binary Serialization or XML Serialization? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
If i have a class that contains some string, int and List type properties and I want to store that class object into my physical disc. So, which method is better performance wise and file size wise.
There is no correct answer really. Performance-wise the binary method is faster and the file size will be smaller, XML well be slower to parse and will have a bigger size because of it adds "metadata" to describe things.
But it really a question of application, the XML is human-readable and more universal while the binary format depends on your implementation of it and cannot be read by others if they don't have your program/specification for this file (unless of course they reverse engineer it...)
Also binary format gives you more freedom in a matter of compression/encryption etc...
I agree with Jonathon and Urilil.
While binary is more efficient for performance and file size, I would not recommend to use it.
You will face two issues :
- i case of a bug, how do you know if the bug is in the write or in the read side ?
- when your class evolves (and it will), you might not be able to read old files whith last version of your application
So, use XML or JSON.
if you still need binary, have a look at protocol buffers
It actually depends on the context. If you have two applications using same data, you might use Binary Serialization which is faster but less flexible. But in case of different systems, like one in C# another in Python, XML serialization is more appropriate for its flexibility. And you have to keep performance issues in mind. So, there is always a tradeoff.

Should I really Pascal case almost all public identifiers in C#? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm coming to C# from java. Java has a set of conventions that make it easy to see the use of an identifier by its casing.
Reading up on the Microsoft conventions it seems like they want you to Pascal case almost everything except parameters. It seems to me this is a much less information rich convention.
Is it really considered bad form in the .NET/Mono world to break this convention and do I just need to get used to it, or can I carry my Java habits over to my C# code that others are going to use?
Well, ultimately it depends on you what you choose. If others are ok with the Java style of naming, you can use it, but I would probably stick to pascal case at least for public members your classes to be consistent with the rest of the language.
ReSharper offers quick way to convert naming conventions be reformatting code, so you might also try to use your learnt ways for new classes and let it perform it's magic before you check in the code for others to see :-) .
Personally I think that the name of the function is far more important than it's casing, as well as it's XML comments. Even if you don't stick to any conventions, nobody will have a problem in using your code as long as these 2 things are in order. And on the other hand - no amount of tradition is going to help with DoMagic2_2(int a, string b, decimal bb). So focus on that and use whatever convention is easier for you.
It all depends on readers of your code. If you're the only one reading it, or the other readers are going to be Java-versed, it might not be a problem. Then again, if your code readers will be more C#y people, your code will look strange to them, and might confuse them.
I recommend going with the convention as it makes your code more civilized. Given that there will be more C# developers reading (or) will be interested in reading your C# code rather than Java developers. For instance, if you want to post things in a blog or a thread like StackOverflow, your code will make more sense / won't look too foreign to corresponding developers if you follow the convention. Also, I believe adapting to a specific convention isn't tough at all. I'm a C# developer and I currently work on Rails, so take my word on it.
I don't find why it should be less information, anyway the casing don't tells everything, but the most information about what you're dealing with comes from the syntax and usage instead. Although I never done any serious Java, so far they're not that different, probably the most notable being the method names being lowercase (Java) vs uppercase (C#).
In any case, they're just conventions, and like any convention, they're to some extent arbitrary. It really depends on what you prefer more, who will have to read your code, and what code is already on the codebase. The most important thing about conventions is to make all the code look the same, in whatever style you/others like.
If you already have code written (by you or by others) and have to modify that, by all means stick to whatever it's already there, just for the sake of consistency. But if starting from scratch, you may either want to decide to follow Java standard for your own comfort or C# standard for ease of reading by others. Or, why not decide with your team on a "middle ground" custom convention?, so that everyone is happy with it.

C# versus XSLT for transforming XML data to html file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am working on a project that generate HTML. The following are the steps.
Reads an XML file then update the XML file with some data
Generate the final XML
Transform this final XML using XSLT to HTML file -
The size of the XML is big and the transformed HTML file will have more than 10 printable pages(this is just to show you how big the single html file is). Beside the solution has to transform more than 4000's of different XML files. As the conditions of the HTML view for different products getting bigger, its becoming hard to satisfy using the XSLT Conditional statements and Templates. Its not that flexible to abstract whats common and differentiate across different HTML views. I was thinking to use C# to do the transformation instead of XSLT. But its gonna be a big task to implement all the XSLT job to a C# classes so I would like to know if its convincing to do it using C#. or if its a better option for my case.
Do you have any suggestions on this? Please let me know what you think: if i better clean the xslt and keep using it or I should use a new C# implementation.
Thanks
Sounds like the project grew in some unexpected ways and you may be well served by doing a re-write. The next version of the project will be better cause you know more about the project, and how it's changing based on your experience.
Re-write it using the "best" technology, where "best" is defined by what your development team knows "best". The solution can be equally clean in either C# or XSLT.
A 10 page HTML document isn't really very big, nor are 4,000 of them, XSLT would be fine. Be sure that you're not calling too many templates (ie the template stack isn't too deep - more than 4 or 5 layers is deep), and that you're using <xsl:variable>s to eliminate multiple look-ups through the source XML to speed things up.
XSLT can be very clean and concise... But you'll need to keep it clean if you want it to perform well especially on the scope your speaking of. On the other hand i've imported large XML files into collections and it worked fairly efficiently. Its really personal preference. Considering that you are generating HTML as end result... Id probably stick with XSLT.
Note: many developers are not as familiar with XSLT as other data transport languages... You may want to take that into consideration if others will be working with this often
XSLT is ideal for transforming XML to HTML or other XML. None of the concerns you raise are true obstacles; each has simple solutions that are well known by most XSLT developers:
You don't mention specific performance requirements, but tens of
printable pages and thousands of XML input files isn't excessive for
XSLT projects.
Proliferation of conditions is no problem for XSLT templates. More
specific templates take precedence, and the predicate language is so
rich and well-tuned to XML that complex conditional processing is
actually a strength, not a liability of well-written XSLT programs.
Commonly used templates can be factored out of XSLT files and
maintained separately in files that are easily imported or included
where needed.
More elaborate transformations can leverage multiple passes or staged
application of templates (but I don't yet hear that any of your needs
require such techniques).
I strongly suggest that you stay the course with XSLT for your XML transformation project. You may have the basis for a few separately asked Stack Overflow questions in your concerns, but nothing you've written is reason to switch to another language.

Categories