Search code examples
c#xml.net-4.0xmlreader

Load fail directory file in XmlReader .NET 4.0


This filename has character is  (0xE700).

when i read in XmlReader, i can't read it because the file name load changes character is %EE%9C%80

XmlReader reader = XmlReader.Create(fileName, settings);

Why  (0xE700) => %EE%9C%80!


Solution

  • it happens since XmlReader.Create treats first parameter as Uri. This is not obvious but you can get it from the method signature

    XmlReader Create(string inputUri, XmlReaderSettings settings, XmlParserContext inputContext);
    

    0xE700 is a special character that can't be used in the Uri and is escaped to %EE%9C%80!

    However you can change the code and read the file content using FileStream e.g.

    var fileName = string.Format("{0}test.xml", char.ConvertFromUtf32(0xE700));
    File.WriteAllText(fileName, "<root><node /></root>");
    using (var fileStream = new FileStream(fileName, FileMode.Open))
    {
        using (var reader = XmlReader.Create(fileStream))
            reader.ReadStartElement();
    }
    
    

    new FileStream(fileName, FileMode.Open) will read file by name and will not encode the file name (as XmlReader.Create does).