How do I validate an XML document against a DTD in C#?

前端 未结 4 1757
长发绾君心
长发绾君心 2021-01-05 07:21

I don\'t want to do anything fancy, I just want to make sure a document is valid, and print an error message if it is not. Google pointed me to this, but it seems XmlValidat

4条回答
  •  孤城傲影
    2021-01-05 07:55

    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

    Lambda Expressions

    Type Inference

提交回复
热议问题