XMLReader.ReadInnerXML() throws invalid character/Hexa decimal errors

  • Thread starter Thread starter Avatar 123
  • Start date Start date
A

Avatar 123

Guest
Hello Every one,

I have a file which contains below text (Some times this file size may grow up to 200 MB)

<Test>.... </Test> 0x00 (Hexa decimal character)

<Test>.... </Test>

<Test>.... </Test>

Below code works fine when there is no Invalid characters and when there is a invalid character/Hexa decimal value it throws an exception at reader.ReadInnerXml(); saying " '.', hexadecimal value 0x00, is an invalid character. Line 1, position 658."

Can some one please help me in how i can remove special character or avoid exception or ignore special character.


try
{
string path = @"C:\Users\xxxxx\Desktop\Test.log";
var tmpFileStream = new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
//var memoryStream = new MemoryStream();
var xmlDoc = new XmlDocument();
var settings = new XmlReaderSettings { CheckCharacters = false, IgnoreWhitespace = true };
using (var reader = XmlReader.Create(new StreamReader(tmpFileStream), settings))
{

//tmpFileStream = null;
while (reader.Read())
{
if (reader.NodeType != XmlNodeType.Element || reader.Name != "Test") continue;

var innerXml = reader.ReadInnerXml();
}
}
}
catch (Exception ex)
{

}

Continue reading...
 
Back
Top