How to validate XML in .NET Core 1.1.2

2019-03-05 21:55发布

How can i validate XML against XSD schema in .NET Core 1.1.2? i found this ms docs but i cannot use it with .NET core 1.1.2

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;

namespace MyNameSpace
{
    public static class XmlValidation
    {   

        public static void Validate(string schema, string xml)
        {
            XmlReaderSettings schemaSettings = new XmlReaderSettings();
            schemaSettings.Schemas.Add(schema);
            schemaSettings.ValidationType = ValidationType.Schema;
            schemaSettings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
            XmlReader reader = XmlReader.Create(xml, schemaSettings);
            while (reader.Read()) { }
        }

        static void ValidationEventHandler(object sender, ValidationEventArgs e)
        {
            // do something
        }
    }
}

I am getting errors

The type or namespace name 'ValidationEventHandler' could not be found
The type or namespace name 'ValidationEventArgs' could not be found
The name 'ValidationType' does not exist in the current context Domain
'XmlReaderSettings' does not contain a definition for 'Schemas' and no extension method 'Schemas' accepting a first argument of type 'XmlReaderSettings' could be found (are you missing a using directive or an assembly reference?)

am i missing any Nuget Package here or .NET Core 1.1 does not even support xml validation?

1条回答
Root(大扎)
2楼-- · 2019-03-05 22:38

It does not. Here is the XmlReaderSettings class in .NET Core 1.1. There are is no ValidationEventHandler under Events. Here is the same class in .NET Core 2.0 where it is present.

查看更多
登录 后发表回答