How to validate XML in .NET Core 1.1.2

与世无争的帅哥 提交于 2019-12-18 09:46:43

问题


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:


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.



来源:https://stackoverflow.com/questions/48835738/how-to-validate-xml-in-net-core-1-1-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!