I understand that there are already many existing threads for the 'element not found' error for XML validation but I'm posting this as my problem is slightly different.
My code was running fine until it was on .Net v4.5. After upgrading the framework to v4.6.1, I started getting this error for each XML element in my xml file. Upon downgrading I found that the error started occurring from .Net framework v4.5.2 onwards. Below is my code:
XML - Book.xml
<?xml version="1.0" ?><!DOCTYPE book SYSTEM "Book.dtd"><book><title>The Lord of the Rings</title><author>J.R.R. Tolkien</author><isbn>1572810556</isbn></book>
DTD - Book.dtd
<!ELEMENT book (title, author, isbn)><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT isbn (#PCDATA)>
C# Functions
public static void ReadXMLwithDTD() { // Set the validation settings. XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.DTD; settings.DtdProcessing = DtdProcessing.Parse; settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); settings.IgnoreWhitespace = true; // Create the XmlReader object. XmlReader reader = XmlReader.Create("Book.xml", settings); // Parse the file. while (reader.Read()) { Console.WriteLine("{0}, {1}: {2} ", reader.NodeType, reader.Name, reader.Value); } } private static void ValidationCallBack(object sender, ValidationEventArgs e) { if (e.Severity == XmlSeverityType.Warning) Console.WriteLine("Warning: Matching schema not found. No validation occurred." + e.Message); else // Error Console.WriteLine("Validation error: " + e.Message); }
When the code is executed the callback throws validation errors (element not found) for all four XML elements - book, title, author and isbn.
I also found that DTD is not read. I introduced errors in the dtd file, even renamed the file, there was no change. Next, I reverted the errors and switched the framework back to 4.5, the code worked as expected. With the framework still set at 4.5, I re-introduced the errors and the code threw errors as applicable.
Please help, I need to fix this urgently.
Thanks,