Illegal Characters in XML document

D

Duncan Welch

Hi,

I'm using the XmlTextReader to read a badly formatted XML document from a
URL (it has no <?xml tag) on a remote website (not mine). About halfway
through, I get an Illegal Character exception, which I'm assuming means that
the default encoding type is wrong.

Does anyone know a way to override the encoding type on the XmlTextReader?

Thanks in advance,

Duncan
 
J

Jon Skeet [C# MVP]

Duncan Welch said:
I'm using the XmlTextReader to read a badly formatted XML document from a
URL (it has no <?xml tag) on a remote website (not mine). About halfway
through, I get an Illegal Character exception, which I'm assuming means that
the default encoding type is wrong.

Does anyone know a way to override the encoding type on the XmlTextReader?

Well, you could download the document and then use a StreamReader to
convert the encoding. That might not be the problem though - it might
be something like a < within text (instead of &lt;). I'd try to look at
the place where the error occurs to check before going to too much
other trouble.
 
S

Shakir Hussain

The default encoding type on the XmlTextReader is UTF-8, if no encoding type
is specified in the xml file.

Try this to change the default encoding type

//open a file stream
FileStream fstream= new FileStream(@"c:\myXml.xml, FileMode.Open,
FileAccess.Read);

//initialize a XmlParsercontext and specify the encoding type
XmlParserContext myParserContext = new XmlParserContext(null, null, "",
XmlSpace.Default, System.Text.Encoding.UTF7);

//init XmlTextReader
XmlTextReader xtr = new XmlTextReader(fstream, XmlNodeType.Document,
myParserContext );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top