可能性のある実行時にコードを使用してXSDに対してXMLを検証するには?

StackOverflow https://stackoverflow.com/questions/1277595

質問

私は、実行時にXSDファイルに対してXMLを検証することが可能であるが、私は、実行時に読み込むXMLファイルを持っていますか? C#を使用して

役に立ちましたか?

解決

これを試してください:

public void ValidateXmlDocument(
    XmlReader documentToValidate, string schemaPath)
{
    XmlSchema schema;
    using (var schemaReader = XmlReader.Create(schemaPath))
    {
        schema = XmlSchema.Read(schemaReader, ValidationEventHandler);
    }

    var schemas = new XmlSchemaSet();
    schemas.Add(schema);

    var settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.Schemas = schemas;
    settings.ValidationFlags =
        XmlSchemaValidationFlags.ProcessIdentityConstraints |
        XmlSchemaValidationFlags.ReportValidationWarnings;
    settings.ValidationEventHandler += ValidationEventHandler;

    using (var validationReader = XmlReader.Create(documentToValidate, settings))
    {
        while (validationReader.Read())
        {
        }
    }
}

private static void ValidationEventHandler(
    object sender, ValidationEventArgs args)
{
    if (args.Severity == XmlSeverityType.Error)
    {
        throw args.Exception;
    }

    Debug.WriteLine(args.Message);
}

他のヒント

私はCODE TOOです!私は私のテストでこれを使用します:

    public static bool IsValid(XElement element, params string[] schemas)
    {
        XmlSchemaSet xsd = new XmlSchemaSet();
        XmlReader xr = null;
        foreach (string s in schemas)
        { // eh, leak 'em. 
            xr = XmlReader.Create(
                new MemoryStream(Encoding.Default.GetBytes(s)));
            xsd.Add(null, xr);
        }
        XDocument doc = new XDocument(element);
        var errored = false;
        doc.Validate(xsd, (o, e) => errored = true);
        if (errored)
            return false;

        // If this doesn't fail, there's an issue with the XSD.
        XNamespace xn = XNamespace.Get(
                      element.GetDefaultNamespace().NamespaceName);
        XElement fail = new XElement(xn + "omgwtflolj/k");
        fail.SetAttributeValue("xmlns", xn.NamespaceName);
        doc = new XDocument(fail);
        var fired = false;
        doc.Validate(xsd, (o, e) => fired = true);
        return fired;
    }

この1は、文字列(アセンブリ内のファイルリソース)としてのスキーマに取り、スキーマセットに追加します。私が検証し、その有効でない場合、私はfalseを返します。

XMLが無効であることが判明していない場合、私は私のスキーマはめちゃくちゃにされていないことを確認するために、負のチェックを行います。その確実な保証はありませんが、私は自分のスキーマ内のエラーを検出するために、これを使用しています。

簡単な解決策..

        try
        {
            XmlReaderSettings Xsettings = new XmlReaderSettings();
            Xsettings.Schemas.Add(null, "personDivideSchema.xsd");
            Xsettings.ValidationType = ValidationType.Schema;

            XmlDocument document = new XmlDocument();
            document.Load("person.xml");

            XmlReader reader = XmlReader.Create(new StringReader(document.InnerXml), Xsettings);


            while (reader.Read());
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message.ToString());
        }

は、このリンクがお役に立てば幸います:

P>

scroll top