How to deal with strings that contain invalid XML characters [duplicate] - c#

This question already has answers here:
Dealing with forbidden characters in XML using C# .NET
(6 answers)
Closed 8 years ago.
We have an app that collects data electronically and by user input. The data is eventually turned into XML. We have had problems with invalid XML characters being in the inbound data when we turn it into XML either by serializing objects or using a .Net Transform. The process will thrown an exception like the below.
Exception: System.Xml.XmlException: '', hexadecimal value 0x10, is an invalid character. Line 5, position 74.
I don't know any other way to fix this other than scrubbing all the data either at input time or at the time the XML is created. The thought of running every string input or string property in an object through a cleaning function doesn't sound appealing. Is that the way this would need to be resolved.
Looking for confirmation or alternatives.
Thanks,
Kevin

There really isn't an elegant solution for this, but this response has some examples of whitelist cleansers.

Related

Error received Bad JSON escape sequence [duplicate]

This question already has answers here:
json error Bad JSON escape sequence
(2 answers)
Closed 4 years ago.
I'm currently testing an application but it's throwing a Bad JSON Escape Sequence at me, however I don't see the problem...
I'm probably overlooking something so a fresh pair of eyes might be useful.
messageContents = "{\"command\":\"cue\",\"channel\":1,\"uid\":\"aesd-deaf\",\"type\":\"standard\",\"waitforexecute\":true,\"duration\":0,\"scene\":[{\"name\":\"Scene1\",\"fields\":[{\"Quad1\":\"F:\\TestFolder\\mill.jpg\"}]}]}";
And the error I'm getting is
{"Bad JSON escape sequence: \\T. Path 'scene[0].fields[0].Quad1', line 1, position 150."}
Can anyone spot the mistake?
Thanks,
Kenneth
Like the error says, the problem happens inside the array for the fields property:
[{\"Quad1\":\"F:\\TestFolder\\mill.jpg\"}]
Imagine what this looks like, once parsed:
[{"Quad1": "F:\TestFolder\mill.jpg"}]
The JSON parser doesn't recognize the escape sequence \T, which is not the same as \t.
To fix is simply double escape all the \ characters. So that section would look like:
\"fields\": [{\"Quad1\":\"F:\\\\TestFolder\\\\mill.jpg\"}]

How to split concatenated JSON files using C# [duplicate]

This question already has an answer here:
What is the correct way to use JSON.NET to parse stream of JSON objects?
(1 answer)
Closed 4 years ago.
I've got to process files that are full of JSON objects. These have simply been concatenated together with no separator thus making the whole file invalid JSON. What is the best way to split this up again? I need to ensure that I don't end up splitting in encoded strings and it needs to be fairly fast as the file can be quite big.
Example file:
{"property":"Data which may include}{"}{"property":"A second object"}
I've done a lot of parsing like this. There's so much JSON code out there that it's rarely necessary with JSON. But if you really need to pass this code yourself in C#, I see no way to approach this other than by manually parsing it character by character.
Special attention needs to be given to curly braces and colons. And, when parsing tokens you'll need to determine if it's quoted. If it's quoted, then you go until the closing quote (ignore any escaped quotes). If it's not quoted, then you go until you hit a non-symbol character.
You might find this task a little easier using my Text Parsing Helper Class class to handle some of the lower-level string handling of your parser.

Reading appended line from bottom of logfile [duplicate]

This question already has answers here:
Read last line of text file
(6 answers)
Closed 8 years ago.
Scenario is the following:
A (weather) service dumps sensor data into a log file/text file.
The new readings are appended to the bottom of a given (existing) file
New data is added at regular intervals (interval may or may not be known)
I need to parse the new information/line and send it off to another service.
I don't want to read the whole file every time, unless I have to.
EDIT: Sorry for the bad wording. "unless I have to" should be understood as if there is no other way around. I have seen the post/answer referenced and it seems a little extensive.
Framework is 4.5.x.
Thank you.
To get the the last line of a text file you can use this
File.ReadLines(myFileName).Last();
This is the simplest method, but is inefficient. You can write your own parser as show here

C# is valid UTF-8 [duplicate]

This question already has answers here:
Determine a string's encoding in C#
(10 answers)
Closed 9 years ago.
I have a string read as a UTF8 (not from a file, can't check BOM).
The problem is that sometimes the original text was formed with another encoding, but was converted to UTF8 - so the string is not readable, sort of gibberish.
is it possible to detect that this string is not actual UTF8?
Thanks!
No. They're just bytes. You could try to guess, if you wanted, by trying different conversions and seeing whether there are valid dictionary words, etc., but in a theoretical sense it's impossible without knowing something about the data itself, i.e. knowing that it never uses certain characters, or always uses certain characters, or that it contains mostly words found in a given dictionary, etc. It might look like gibberish to a person, but the computer has no way of quantifying "gibberish".

Merging WAV files in c# [duplicate]

This question already has answers here:
How to join 2 or more .WAV files together programmatically?
(5 answers)
Closed 9 years ago.
I am working on Regional Language Text to Voice converter in C#. I have wav files for individual characters.
I want to merge them to get a resultant word's single WAV file.
Im using NAudio library. It supports the concatenate method which takes outputFile and IEnumerable string sourceFiles as arguments.
But when I execute the method I get NullArgumentException in the line which creates WavFileReader object.
But I have a passed a string of array containing filepaths and an existing empty output wavfile as arguments. I am stuck here.
Could you please suggest me how to use this method,as in how to call this method? and what arguments to use...
There is no Concatenate method in NAudio. I assume you are referring to my answer here. The output WAV file should not already exist - it will be created for you. But the most common cause for an ArgumentNull exception is a null parameter being passed into the function. Are you completely sure your source files array does not contain a null. (adding the stack trace of the exception to your question might help us diagnose further).

Categories