如何在 .NET Core 1.1.2 中验证 XML [英] How to validate XML in .NET Core 1.1.2

查看:21
本文介绍了如何在 .NET Core 1.1.2 中验证 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据 .NET Core 1.1.2 中的 XSD 架构验证 XML?我发现了这个 msdocs 但我不能将它与 .NET core 1.1.2 一起使用

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
        }
    }
}

我收到错误

找不到类型或命名空间名称ValidationEventHandler"
找不到类型或命名空间名称ValidationEventArgs"
当前上下文域中不存在名称ValidationType"
XmlReaderSettings"不包含Schemas"的定义和没有接受类型的第一个参数的扩展方法模式"可以找到XmlReaderSettings"(您是否缺少 using 指令或程序集参考?)

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?)

我在这里缺少任何 Nuget 包还是 .NET Core 1.1 甚至不支持 xml 验证?

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

推荐答案

它没有.这里是.NET中的XmlReaderSettings类核心 1.1.Events 下没有 ValidationEventHandler.这里是.NET中的同一个类Core 2.0 存在的地方.

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.

这篇关于如何在 .NET Core 1.1.2 中验证 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆