Quantcast
Channel: How do I safely create an XPathNavigator against a Stream in C#? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

How do I safely create an XPathNavigator against a Stream in C#?

$
0
0

Given a Stream as input, how do I safely create an XPathNavigator against an XML data source?

The XML data source:

  • May possibly contain invalid hexadecimal characters that need to be removed.
  • May contain characters that do not match the declared encoding of the document.

As an example, some XML data sources in the cloud will have a declared encoding of utf-8, but the actual encoding is windows-1252 or ISO 8859-1, which can cause an invalid character exception to be thrown when creating an XmlReader against the Stream.

From the StreamReader.CurrentEncoding property documentation: "The current character encoding used by the current reader. The value can be different after the first call to any Read method of StreamReader, since encoding autodetection is not done until the first call to a Read method." This seems indicate that CurrentEncoding can be checked after the first read, but are we stuck storing this encoding when we need to write out the XML data to a Stream?

I am hoping to find a best practice for safely creating an XPathNavigator/IXPathNavigable instance against an XML data source that will gracefully handle encoding an invalid character issues (in C# preferably).


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images