【问题标题】:How to validate XML in .NET Core 1.1.2如何在 .NET Core 1.1.2 中验证 XML
【发布时间】:2018-02-16 22:28:02
【问题描述】:

如何在 .NET Core 1.1.2 中针对 XSD 架构验证 XML?我找到了这个ms docs,但我不能将它与 .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 指令 还是程序集参考?)

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

【问题讨论】:

    标签: .net-core asp.net-core-1.0 coreclr system.xml


    【解决方案1】:

    它没有。 Here is the XmlReaderSettings class in .NET Core 1.1。在Events 下没有ValidationEventHandlerHere is the same class in .NET Core 2.0 它所在的位置。

    【讨论】:

    • 那么我们如何在 .net core 1.1.2 中验证 Xml?
    • @LP13:你为什么不升级到.Net core 2.0?。从 .Net core 1.1.2 升级到 2.0 真的很简单
    • 这就是我最终做的事情。
    猜你喜欢
    • 2018-04-06
    • 2019-10-13
    • 2018-02-04
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 2021-01-13
    • 2021-07-24
    • 1970-01-01
    相关资源
    最近更新 更多