Convert from System.Byte[] to String fails - c#

A product that we use is storing the Letter Express (Mail Merge) template as a BLOB (or CLOB...I cannot remember right now). We use the Product's API to call this letter express and send an email.
Now, we want the content of the email to be captured and stored in a separate field. The API provides us with a LetterExpress.WordDocument property which has the template. This is however a byte[].
I am trying to get this into a string object so that I can populate the place holders and then store it in a different field.
This is the code that I was trying.
System.Text.Encoding.ASCII.GetString(LetterExpress.WordDocument)
However, I get an error as follows
The best overloaded method match for
'System.Text.Encoding.GetString(byte[])'
has some invalid arguments
Why am I getting this error?
How can I ascertain what is the encoding that is being used for the LetterExpress.WordDocument? Or is there a generic method that can convert it into a string?

You're getting that error because the LetterExpress.WordDocument property you think is a byte[] really isn't one. Verify that the type of that property really is what you think it is.

It sounds like this is an actual .doc file, and a .doc file is much more complicated than just a string encoding. If you want to extract the text from a word document, you need something like the Aspose Tools. The ability to do this is not built into the framework. There is not System.Text.Encoding you can use, and no generic method including with .Net that can do this.

Related

How to convert a string to html attribute value as interpreted by a browser in C#

I need to validate user input for an href on the server side and need to make sure only http:// and https:// are allowed as a protocol (if specified at all.) The objective is to eliminate possible malicious code like javascript:... or anything alike.
What makes it difficult is the number of ways the colon could be encoded in such string e.g. :, &#58, :, &#x0003A , :. I'd like to transform the value and see it as the browsers do before they render the page.
One option could be building a DOM document using AngleSharp as it does the perfect job when parsing attributes. Then I could retrieve the value and validate it but it seems somewhat of an overkill to build the whole DOM tree just to parse one value. Is there a way to use AngleSharp to parse just an attribute value? Or is there a lib which I could use just for this task?
I also found this question, but the method used in there does not really parse the URIs the way browsers do.
You want the HtmlDecode() method. You may need to add a reference to the project to use it.

C# easiest way of storing strings to external file

C#
TL;DR: I want the user to be able to input text, which is then written to an external file, which then can be called later based on position in the file. What's the easiest way and form to read and write a list of strings to?
Very amateur question, but I can't seem to find an easy anwer on the internet. If I'm missing something obvious, please redirect me. I am writing a very simple program in which the user can input a string which is then written (added) to an external file, from which later a string can be called based on the position in the file. I found things like JSON, Resource file, SQL DB... Problem is that due to my lack of programming experience I have no idea what's the best option to look into.
Example of what I want to achieve:
User inputs strings 'Dog', 'Cat', and 'Horse' into the textbox. Each of these strings are added to the external file. Lateron, the user calls the 2nd number on the list, to which the program returns 'Cat'.
Thanks!
If you already know the kind of data that will be saved I recommend using XML Serialization. This lets you save and read your file very easily. The linked example is from Microsoft and shows a dataset being serialized. If you want to save a generic list instead of a fixed object you might find this link helpful.
Alternatively, you could save data to your application configuration file (search online for "C# application configuration for PROJECT_TYPE" where the project type is winforms/mvc/class library etc..)

c# decode HTML chars in email

An array contains an email. This e-mail looks at the moment like:
testmail%40mail.de
I'm searching for an methode to get this e-mail to readable format for humans. I know everybody would be able to identify '%40' after some time, but I would need to get for example in this case 'testmail#mail.de'
I found the solution to use "System.Net.WebUtility.HtmlDecode()". I use .Net4.0 but there was no readable output using the methode above (output was the same one). I hope to get an easy way to solve this.
The email address is URL encoded therefore you are required to URL decode it.
.NET has the following utilities available for you to do this:
HttpServerUtility.UrlDecode(string) or HttpUtility.UrlDecode(string)

Pass URL with single quote and ampersand in Outlook using a .NET website

I have a web page that uses a webmail service to send emails. This is on an company intranet using a Microsoft Exchange server. My website created an email with a link to an image handler on my website. In my code, I can print some debug messages and I see:
<img src='http://tav.target.com/VIBEHandler.ashx?id=z064441_45975&type=Amazing'/>
But in the email, when I view the source code, I see this:
<img src="http://tav.target.com/VIBEHandler.ashx?id=z064441_45975&type=Amazing"/>
My single quotes changed to double quotes (no big deal).
&
changed to
&
This causes the URL to not work and images appear as the red "x", indicating a missing image.
How can I preserve my URL?
Your 3rd party emailing service might be converting your HTML document to a valid XML document for compatibility reasons.
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
Basically, in XML, an ampersand character represents and XML entity, and can not be used unless you place the text within a CDATA node. Your 3rd party service seems to just be converting the & to & , which would work to safely display the value, but doesn't do too much for a URL.
http://www.w3schools.com/xml/xml_cdata.asp
If I were in your situation, I would URL encode the image URL when generating the HTML document that is being sent out. This way, it is both a proper link, and a valid XML string.
HttpUtility.UrlEncode(myUrlString);
http://msdn.microsoft.com/en-us/library/4fkewx0t%28v=vs.110%29.aspx
Hope this helps!
The best solution we could come up with is to use a single variable with multiple values separated with an underscore. This eliminates the need for the '&' symbol entirely and makes everything happy and compatible.
The URL is basically a link to an image handler so we can include images in emails without the use of attachments, shared drives, etc. The image handler can also do things like merge images together to create a single image (WAY better than trying to overlap images in emails which almost NEVER works). I simply added some code to the image handler that can check for and dissect the "meta-variable" in my URL.
http://sample.com?var=ONE_TWO_THREE
http://sample.com?var1=ONE&var2=TWO&var3=THREE
The URL now looks more clean and can have as many variables as I want so long as I put everything in the exact correct order, read it all in using the same sequence, don't miss anything, and document everything well. I COULD go one step farther and specify what each variable means:
http://sample.com?var=first-Nicolai_last-Dutka_age-34_etc-foobar
But that just tells the whole world what all my variables mean! Hypothetically, I could do:
http://sample.com?var=24154#kja&nl897q45pjkh8&&^HJ435
Then it would be up to me to determine where the breaking points are to bust that up into the variables:
24151, kja*, n1897, 45, etc
Of course, I'm not going to be that complex and will likely just stick to:
http://sample.com?var=ONE_TWO_THREE
Enjoy!

Get parameter name/value using rs:StoredParametersID key?

I'm working with SSRS 2008 R2 with the ReportService2010 soap endpoint with the ReportExecutionService.
For parameters that have large values, SSRS has some smarts to automatically tokenize a drill-down report URL (summed up nicely in this thread):
The problem is related to the RS System Property
StoredParametersThreshold. This value (defaults to 1500) determine
the number of characters a URL can contain before SSRS replaces that
URL with a token. The reason for this functionality is that some
browsers/servers limit the URL length of a URL. Since report
parameter value combinations can be really long, we try to be smart
and tokenize the URL so that the URLs will work. A possible work
around is to increase the value of StoredParametersThreshold.
The URL (decoded for easier viewing) looks something like this:
http://iprod-ssrs/ReportServer?http://iprod-reports/admin/web/Report+Library/Drill-down+Companyids.rdl&rs:StoredParametersID=cjesl5vk0y2tbv55e1qjrz55&rs:ParameterLanguage=&rc:Parameters=Collapsed
The problem is, in my custom viewer, I cannot use these tokenized URLs and need to retrieve the actual parameter name/value so that I may execute the drill-down report.
Is it possible to get the parameter values using the rs:StoredParametersID key?
According to this post from 2010 you can't retrieve the actual parameters http://social.msdn.microsoft.com/Forums/en/sqlreportingservices/thread/6809f9e3-b411-4f05-96a6-47e273220a70 though maybe that's changed. Good luck!

Categories