针对C#中引用的XSD验证XML


161

我有一个具有指定架构位置的XML文件,例如:

xsi:schemaLocation="someurl ..\localSchemaPath.xsd"

我想在C#中验证。当我打开文件时,Visual Studio会根据架构对其进行验证并完美列出错误。但是,不知何故,我似乎无法在C#中自动验证它,而无需指定要验证的架构,如下所示:

XmlDocument asset = new XmlDocument();

XmlTextReader schemaReader = new XmlTextReader("relativeSchemaPath");
XmlSchema schema = XmlSchema.Read(schemaReader, SchemaValidationHandler);

asset.Schemas.Add(schema);

asset.Load(filename);
asset.Validate(DocumentValidationHandler);

我是否应该能够自动使用XML文件中指定的模式进行验证?我想念什么?


1
请参阅MSDN的示例:msdn.microsoft.com/en-us/library/...

Answers:


167

您需要创建一个XmlReaderSettings实例,并在创建它时将其传递给XmlReader。然后,您可以订阅ValidationEventHandler设置中的以接收验证错误。您的代码最终将如下所示:

using System.Xml;
using System.Xml.Schema;
using System.IO;

public class ValidXSD
{
    public static void Main()
    {

        // Set the validation settings.
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

        // Create the XmlReader object.
        XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);

        // Parse the file. 
        while (reader.Read()) ;

    }
    // Display any warnings or errors.
    private static void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.WriteLine("\tWarning: Matching schema not found.  No validation occurred." + args.Message);
        else
            Console.WriteLine("\tValidation error: " + args.Message);

    }
}

4
+1尽管应该更新为使用using条款以确保完整性:)
IAbstract

55
如果要与XSD文件进行比较,请在上面的代码中添加以下行:settings.Schemas.Add(“ YourDomainHere”,“ yourXSDFile.xsd”);
杰夫·佛尔

5
要获取错误的行号和位置号,只需使用:validsCallBack中的args.Exception.LineNumber ...
user610064,2012年

1
如果我拥有的架构没有名称空间怎么办?

1
使用lambda,更好的恕我直言,更清晰的代码 settings.ValidationEventHandler += (o, args) => { errors = true; // More code };
Kiquenet

107

如果使用的是.NET 3.5,一种更简单的方法是使用XDocumentXmlSchemaSet验证。

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(schemaNamespace, schemaFileName);

XDocument doc = XDocument.Load(filename);
string msg = "";
doc.Validate(schemas, (o, e) => {
    msg += e.Message + Environment.NewLine;
});
Console.WriteLine(msg == "" ? "Document is valid" : "Document invalid: " + msg);

请参阅MSDN文档以获取更多帮助。


2
该方法要求您事先了解架构,而不是从xml中获取内联架构。
Lankymart 2013年

这可以正常工作,但是当xml文档包含诸如<catalog> my <i> new </ i> catalog ...之类的html标记时抛出错误。在上述情况下,诸如“ <i>”的html标记会产生问题它是“ <catalog>”的值...如何验证它
Anil Purswani 2014年

6
@AnilPurswani:如果希望将HTML放入XML文档中,则需要将其包装在CDATA中。<catalog><![CDATA[my <i> new </i> catalog....]]></catalog>是这样做的正确方法。
p0lar_bear 2015年

简洁大方!当针对固定的模式集(这是我们的情况,以及一个包含多个文件夹和文件的大型模式)进行验证时,这非常有效。我已经在考虑缓存XmlSchemaSet,以在对Validator的调用之间重用。非常感谢!
Adail Retamal

20

以下示例验证XML文件并生成适当的错误或警告。

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

public class Sample
{

    public static void Main()
    {
        //Load the XmlSchemaSet.
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.Add("urn:bookstore-schema", "books.xsd");

        //Validate the file using the schema stored in the schema set.
        //Any elements belonging to the namespace "urn:cd-schema" generate
        //a warning because there is no schema matching that namespace.
        Validate("store.xml", schemaSet);
        Console.ReadLine();
    }

    private static void Validate(String filename, XmlSchemaSet schemaSet)
    {
        Console.WriteLine();
        Console.WriteLine("\r\nValidating XML file {0}...", filename.ToString());

        XmlSchema compiledSchema = null;

        foreach (XmlSchema schema in schemaSet.Schemas())
        {
            compiledSchema = schema;
        }

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add(compiledSchema);
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
        settings.ValidationType = ValidationType.Schema;

        //Create the schema validating reader.
        XmlReader vreader = XmlReader.Create(filename, settings);

        while (vreader.Read()) { }

        //Close the reader.
        vreader.Close();
    }

    //Display any warnings or errors.
    private static void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.WriteLine("\tWarning: Matching schema not found.  No validation occurred." + args.Message);
        else
            Console.WriteLine("\tValidation error: " + args.Message);

    }
}

前面的示例使用以下输入文件。

<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema" xmlns:cd="urn:cd-schema">
  <book genre="novel">
    <title>The Confidence Man</title>
    <price>11.99</price>
  </book>
  <cd:cd>
    <title>Americana</title>
    <cd:artist>Offspring</cd:artist>
    <price>16.95</price>
  </cd:cd>
</bookstore>

books.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:bookstore-schema"
    elementFormDefault="qualified"
    targetNamespace="urn:bookstore-schema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>

 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

18

我个人比较喜欢没有回调的验证:

public bool ValidateSchema(string xmlPath, string xsdPath)
{
    XmlDocument xml = new XmlDocument();
    xml.Load(xmlPath);

    xml.Schemas.Add(null, xsdPath);

    try
    {
        xml.Validate(null);
    }
    catch (XmlSchemaValidationException)
    {
        return false;
    }
    return true;
}

(请参见Timiz0r在“ 同步XML模式验证?.NET 3.5中的帖子” )


9
回调为您提供了有关xml中哪一行不正确的一些额外信息。此方法是非常二进制的,对还是错:)
FrankyHollywood '16

13

我已经在VB中进行了这种自动验证,这就是我的方法(转换为C#):

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags = settings.ValidationFlags |
                           Schema.XmlSchemaValidationFlags.ProcessSchemaLocation;
XmlReader XMLvalidator = XmlReader.Create(reader, settings);

然后,我settings.ValidationEventHandler在阅读文件时订阅了该事件。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.