How to remove invisible junk characters from a XML file

I want to read some xml files.

when i am opening these files with notepad/wordpad/MS Word or any Browser, it is opening in its original form.But when i tried to execute it with MS DOS, an invisible chacter like “∩╗┐” is seen.
I think ‘∩╗┐‘ is creating an error.i am finding the error “Content is not allowed in prolog.”

Please tell me how can i remove this invisible junk characters from xml file using C# code.

Thanks in advance.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Sure sounds like a byte-order mark. Your question is unclear, but if actually do need to write these files from C# without a byte-order mark, you can specify this by passing false to the UTF8Encoding constructor:

XmlDocument xmlDoc = your_xml_document;
using (TextWriter writer = new StreamWriter(output_filename, new UTF8Encoding(false))
{  
    xmlDoc.Save(writer);
}

Method 2

That might be the UTF-8 byte-order mark. Many text editors (for example Notepad++ and jEdit) allow you specify whether the BOM should be written for UTF-8 or not. With Windows Notepad your only option is to save the file as ANSI.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x