Xmldocument Exception: hexadecimal value 0x1a, is an invalid character

Asked

Viewed 2,393 times

1

I have a project that reads an XML generated by another program in which I don’t have access to the source code. The problem is that the generated XML comes with a special character at the end, and when I try to read the XML to Exception is launched.

Follows the code:

XmlDocument xmlDoc = new XmlDocument();
try
{
   xmlDoc.Load(localMontaXML);
}
catch (Exception ex)
{
   erros.Add(ex.Message);
}
ErrorChecker.Check(erros);

Do you have any way of ignoring that character?

I use Windows Forms with . Net 3.5

  • You could edit the file with StreamWriter and remove this character before opening as a XmlDocument

3 answers

2


It has yes guy, but before you should consider the possibility that the file is coming corrupted or something like that. If you are sure that this file is intact, and that there is only one invalid character at the end for some unknown reason (?), you can play the file in memory except the last character, getting its valid xml.

There are two ways I know how to do this. Note the following code:

METHOD 1

using (MemoryStream ms = new MemoryStream()) // cria um stream de memória
using (var fs = new FileStream(@"C:\sample.xml", FileMode.Open, FileAccess.Read))
// abre o arquivo xml. No caso C:\sample.xml
{
    byte[] bytes = new byte[fs.Length]; // onde ficará o conteúdo do arquivo - 1
    fs.Read(bytes, 0, (int)fs.Length); // lê o arquivo
    ms.Write(bytes, 0, (int)fs.Length-1); // escreve tudo exceto o último byte (length - 1)

    ms.Seek(0, SeekOrigin.Begin); // volta para o início do stream

    XDocument doc = XDocument.Load(ms); // carrega o arquivo em memória
    Console.WriteLine(doc.Root.Value); // teste de leitura
}

METHOD 2

using (var fs = new FileStream(@"C:\sample.xml", FileMode.Open, FileAccess.Read))
// cria um arquivo mapeado em memória a partir do seu FileStream
using (var mmf = MemoryMappedFile.CreateFromFile(fs,"xml",fs.Length,MemoryMappedFileAccess.Read,null,System.IO.HandleInheritability.None,true)) 
{
    // cria um stream que enxerga até o final do arquivo - 1
    using (var str = mmf.CreateViewStream(0, fs.Length - 1, MemoryMappedFileAccess.Read)) 
    {
        XDocument doc = XDocument.Load(str); // lê a partir do stream
        Console.WriteLine(doc.Root.Value);
    }
}

The second uses Memory-Mapped Files.

One detail is that these methods copy the contents of the file to memory, it is not the most performative thing in the world so take care when doing this with a large XML, and never forget the using to "discard" your Stream after use.

Good luck and I hope I helped.

1

In my case, the error that occurred was similar hexadecimal value 0x00, is an invalid Character, maybe the solution is close, changing only the character to be replaced at the end.

When the error occurred it was like this:

xmlObj = new XmlDocument();
xmlObj.Load(arquivo.fullPath);

The correction was:

StreamReader sr = arquivo.OpenText(); //carrega o texto do arquivo
string xmlText = sr.ReadToEnd(); //lê o texto do arquivo e salva na variavel xmlText
xmlText = xmlText.Replace("\0", string.Empty); // retira os caracteres nulos;
xmlObj = new XmlDocument(); // cria o xml
xmlObj.LoadXml(xmlText); // Carrega a string diretamente para ser lida como xml

I hope I can help.

1

Good answer @Conrad Clark

I have a third suggestion in case you don’t know how many more characters the file has at the end.

xmlDoc.LoadXml(System.Text.RegularExpressions.Regex.Match(localMontaXML, @"<[\w\W]+>").ToString());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.