Xml invalid character in given encoding

I'm trying to check my xml against it xsd and get an invalid error character in this encoding. The code I use for verification is below:

private static void ValidatingProcess(string XSDPath, string xml)
    {
        MemoryStream stream =
            new MemoryStream(ASCIIEncoding.Default.GetBytes(xml));

        using (StreamReader SR = new StreamReader(XSDPath))
        {
            XmlSchema Schema = XmlSchema.Read(SR, ReaderSettings_ValidationEventHandler);
            XmlReaderSettings ReaderSettings = new XmlReaderSettings();
            ReaderSettings.ValidationType = ValidationType.Schema;
            ReaderSettings.Schemas.Add(Schema);

            ReaderSettings.ValidationEventHandler += ReaderSettings_ValidationEventHandler;
            XmlReader objXmlReader = XmlReader.Create(stream, ReaderSettings);

            bool notDone = true;
            while (notDone)
            {
                notDone = objXmlReader.Read();
            }
        }
    }

Errors on characters like é, so I guessed that it was the fact that UTF-8 was specified as an encoding or a way to create a MemoryStream in the above code with ASCIIEncoding. I tried changing the encoding of both xsd and xml to UTF-16 and memystream to UTF32, but this seems to have no effect. Any ideas?

+3
source share
1 answer

Do not convert your input string to ASCII if your input string contains non-ASCII characters.

StringReader, XmlReader:

using (var reader = XmlReader.Create(new StringReader(xml), settings)) { ...
+5

Source: https://habr.com/ru/post/1735906/


All Articles