Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removal of Invalid characters in stream

Status
Not open for further replies.

CodingIsFun

Programmer
Apr 9, 2004
134
US
Hi all experts:

I am currently having some issues with a large XML file that I need to parse. I am getting the following error:
System.Xml.XmlException was unhandled
Message="Invalid character in the given encoding. Line 283, position 39709."

Is there anyway to remove all Invalid Encoding Characters from a Stream before I feed it to an XmlReader. I have no control over the Xml File(and it is very large and difficult to deal with), it is generated by another source in which they are very stubborn and will not modify there code to evaluate characters.

Any help would be greatly appreciated.

Thanks in advance..
 
Hi all experts:

I have temporarily solved my problem, any further help would be fantastic.

In order to avoid the invalid character I read the file in with StreamReader and Create a new file with StreamWriter using UTF8 Encoding output.

Then I use the new file to run my parsing routine, now the encoding error is gone.

My question is this, is there anyway to eliminate copying the file by reading then writing to a stream, then pass the UTF8 stream to the XMLReader or am I stuck with copying the file then using the new copy.

This is what I have so far:

MemoryStream memStream = new MemoryStream();
StreamReader reader = new StreamReader(file_name_in, Encoding.UTF8,false,200000);
StreamWriter converted_file = new StreamWriter(memStream,Encoding.UTF8,200000);
((StreamWriter)converted_file).AutoFlush = true;

I'm not sure of the syntax to load memStream with the streamWriter.
converted_file.Write(reader.ReadToEnd()); ????

There has to be a way to do this but Im not the expert.

Thanks in advance, and thank you to everyone who has helped.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top