locked
XmlDocument fails to load external DTD file in .NET 4.5.2 onward RRS feed

  • Question

  • I recently updated a C# Visual Studio project from .NET version 4.0 to 4.7.2 and noticed that an external DTD (Document Type Definition) file could no longer be found by my XmlDocument/XmlReader/XMLReaderSettings objects.

    I tried re-targeting the solution to different .NET versions and narrowed the problem down to 4.5.2.

    Below is a simple example of code that demonstrates the problem:

    If the project targets .NET 4.5.1 then the XML document (store.xml) is read with no problem. However if I target 4.5.2 or later, a validation error message  "Validation Error: The 'store' element is not declared." is seen.

    using System;
    using System.Xml;
    using System.Xml.Schema;
    
    namespace XMLReader
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                // Set the validation settings.
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.DtdProcessing = DtdProcessing.Parse;
                settings.ValidationType = ValidationType.DTD;
                settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(ValidationCallBack);
    
                // Create the XmlReader object.
                XmlReader reader = XmlReader.Create("store.xml", settings);
    
                // Create the XmlDocument
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(reader);
    
                    foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                    {
                        Console.WriteLine(node.InnerText + "\n");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
            }
    
            private static void ValidationCallBack(object sender, ValidationEventArgs e)
            {
                Console.WriteLine("Validation Error: {0}", e.Message);
            }
        }
    }
    

    XML File:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE store SYSTEM "store.dtd">
    <store>
      <item type="supplies">
        <name>paint</name>
        <price>16.95</price>
      </item>
    </store>


    DTD file:

    <!ELEMENT store (item)*> 
    <!ELEMENT item (name,price)>
    <!ATTLIST item type CDATA #REQUIRED>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT price (#PCDATA)>

    If I copy and paste the DTD file into the <!DOCTYPE... expression then it works in all versions of .NET:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE store [
    <!ELEMENT store (item)*> 
    <!ELEMENT item (name,price)>
    <!ATTLIST item type CDATA #REQUIRED>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT price (#PCDATA)>]>
    <store>
      <item type="supplies">
        <name>paint</name>
        <price>16.95</price>
      </item>
    </store

    So it seems to be a problem finding the external DTD file

    Any help would be greatly appreciated. Thanks!



    Wednesday, September 18, 2019 11:08 PM

Answers

  • Hi ukeegold, 

    Thank you for posting here.

    According to your question, I make a test on my side and  reproduce your problem.

    To solve this problem, You need to set XmlReaderSettings.XmlResolver property explicitly. 

                XmlReaderSettings settings = new XmlReaderSettings();
    
                settings.XmlResolver = new XmlUrlResolver();
    
                settings.DtdProcessing = DtdProcessing.Parse;
                settings.ValidationType = ValidationType.DTD;

    Starting with the .NET Framework 4.5.2, this setting has a default value of null.

    Result of my test after setting XmlReaderSettings.XmlResolver property:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ukeegold Thursday, September 19, 2019 6:21 PM
    Thursday, September 19, 2019 5:31 AM