How to check XML document on DTD in C #?

I don’t want to do anything, I just want to make sure the document is valid, and print an error message if it is not. Google pointed me to this one , but it looks like the XmlValidatingReader is out of date (at least what MonoDevelop tells me).

Edit: I'm trying to ask Mehrdad, but I have problems. I think I have most of this, but I cannot find OnValidationEvent anywhere. Where can I get an OnValidationEvent?

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += new ValidationEventHandler(/*trouble is here*/);
XmlReader validatingReader = XmlReader.Create(fileToLoad, settings);
+5
source share
3 answers

, XmlValidatingReader class, XmlReaderSettings object XmlReader.Create method:

var settings = new XmlReaderSettings { ValidationType = ValidationType.DTD };
settings.ValidationEventHandler += new ValidationEventHandler(OnValidationEvent);
var reader = XmlReader.Create("file.xml", settings);

.

P.S. OnValidationEvent - , . , , , XmlReader.

+4
var messages = new StringBuilder();
var settings = new XmlReaderSettings { ValidationType = ValidationType.DTD };
settings.ValidationEventHandler += (sender, args) => messages.AppendLine(args.Message);
var reader = XmlReader.Create("file.xml", settings);

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

ValidationEventHandler

-

+2

:

  • Visual Studio.NET Visual # Console ValidateXml. 1.cs :

    using System.Xml;        // for XmlTextReader and XmlValidatingReader
    using System.Xml.Schema; // for XmlSchemaCollection (which is used later)
    
  • Class1.cs isValid Main :

    private static bool isValid = true;      // If a validation error occurs,
                                             // set this flag to false in the
                                             // validation event handler. 
    
  • XmlTextReader XML- Main, XmlValidatingReader XML :

    XmlTextReader r = new XmlTextReader("C:\\MyFolder\\ProductWithDTD.xml");
    XmlValidatingReader v = new XmlValidatingReader(r);
    
  • ValidationType XmlValidatingReader (DTD, XDR ). DTD :

    v.ValidationType = ValidationType.DTD;
    
  • , . ( MyValidationEventHandler 7):

    v.ValidationEventHandler += 
       new ValidationEventHandler(MyValidationEventHandler);
    
  • XML-. , MyValidationEventHandler . isValid false (. 8). isValid , , .

    while (v.Read())
    {
       // Can add code here to process the content.
    }
    v.Close();
    
    // Check whether the document is valid or invalid.
    if (isValid)
       Console.WriteLine("Document is valid");
    else
       Console.WriteLine("Document is invalid");
    
  • MyValidationEventHandler Main :

    public static void MyValidationEventHandler(object sender, 
                                                ValidationEventArgs args) 
    {
       isValid = false;
       Console.WriteLine("Validation event\n" + args.Message);
    }
    

. , XML . :. Visual Studio.NET ProductWithDTD.xml (, <AuthorName>M soliman</AuthorName>). . :

Validation event
Element 'Product' has invalid content. Expected 'ProductName'.
An error occurred at file:///C:/MyFolder/ProductWithDTD.xml(4, 5).
Document is invalid
-1

All Articles