0

DTD File abc.dtd :::

<!DOCTYPE note
[
  
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

XML File xyz.xml :::::

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "D:\abc.dtd">
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

C# code :::

var messages = new StringBuilder();
            var settings = new XmlReaderSettings { ValidationType = ValidationType.DTD };
            settings.DtdProcessing = DtdProcessing.Parse;
            settings.ValidationEventHandler += (sender1, args) => messages.AppendLine(args.Message);
            FileStream fs = new FileStream(@"D:\xyz.xml", FileMode.Open);
            XmlReader reader = XmlReader.Create(fs, settings);

            // Parse the file. 
            while (reader.Read()) ;

            if (messages.Length > 0)
            {
                // Log Validation Errors
                // Throw Exception
                // Etc.
            }

Here the line while (reader.Read()) ; gives error Expected DTD markup was not found. Line 1, position 3

This error does not come if I copy the dtd statements in the xml file. But i need to put them separate and validate.

0 Answers0