Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 如何在C#windows窗体中根据XSD验证xml_C# 4.0 - Fatal编程技术网

C# 4.0 如何在C#windows窗体中根据XSD验证xml

C# 4.0 如何在C#windows窗体中根据XSD验证xml,c#-4.0,C# 4.0,网络上所有可用的C#代码都只用于读取和加载XML和XSD XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(@"D:\XML\Sample.xml"); xmlDocument.Schemas.Add("http://www.w3.org/2001/XMLSchema", @"D:\XML\Sample.xsd"); xmlDocument.Schemas.Compile(); ValidationEventHandle

网络上所有可用的
C#
代码都只用于读取和加载
XML
XSD

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"D:\XML\Sample.xml");
xmlDocument.Schemas.Add("http://www.w3.org/2001/XMLSchema", @"D:\XML\Sample.xsd");
xmlDocument.Schemas.Compile();

ValidationEventHandler eventhandler = new ValidationEventHandler(ValidationEventHandler);
xmlDocument.Validate(eventhandler);

if (valid == true)
{
    label1.Text = "Xml Got Validated!!";
}

void ValidationEventHandler(object sender, ValidationEventArgs e)
{
    valid = false;
    switch (e.Severity)
    {
        case XmlSeverityType.Error: label1.Text = "Xml Validation Failed".ToString();
            break;
        case XmlSeverityType.Warning: label1.Text = "Xml Has some warning".ToString();
            break;
    }
}

这实际上并不是验证我的
XML
,只是读取,即使我没有传递强制元素,它也会说:“它有效”

我使用以下方法,并且它成功地应用了验证:

var xsd = new XmlSchemaSet();

using (var ms = new StringReader(Resources.MyXsd))
{
    using (var reader = XmlReader.Create(ms))
    {
        xsd.Add(XmlSchema.Read(reader, delegate { }));
    }
}

var document = XDocument.Parse(text);

document.Validate(
    xsd, (o, e) =>
        {
            if (e.Severity == XmlSeverityType.Error)
            {
                validationMessages.Add(e.Exception.Message);
            }
        }
);

这是我从1个月开始尝试的,请帮助:(你能告诉我我的xml和xsd第一行标记应该是什么吗?在xsd中我的目标namspace应该与xml中的命名空间匹配吗?我也这样做了,但仍然没有进行验证。ny1能告诉我xml和xsd的第一行标记的确切语法吗?使用它应该如何,我似乎跳过了验证。甚至运行completel使用不同的xml和xsd,我一点错误都没有。知道为什么会这样吗?