针对C#中的嵌套xsd验证xml文件

时间:2019-06-12 10:20:11

标签: c# xml xsd xml-validation

我有一个Xml文件,该文件需要针对xsd(文件)进行验证,该文件反过来又在其中包含一些xsd,该文件位于与主xsd相同的目录中。我将如何针对Xsd验证xml,以便在验证时也考虑内部xsd?

验证XML的代码

var xdoc = XDocument.Load(TextReader);
var schemas = new XmlSchemaSet();
schemas.Add("example.com/rules-engine-configuration.xsd", xsdFilePath);

                try
                {
                    string msg = "";
                    xdoc.Validate(schemas, (sender, args) =>
                    {
                        msg += args.Severity.ToString() + ":" + args.Message + " Line Number :" + args.Exception.LineNumber + " Line Position:" + args.Exception.LinePosition + Environment.NewLine;
                    });
                }

msg在此处以“”表示,表示其已解析而不是显示异常。

下面是主要的Xsd文件,而xs:include标记包含其他Xsd文件

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="example.com/rules-engine-configuration.xsd"
    elementFormDefault="qualified"
    xmlns="example.com/rules-engine-configuration.xsd"
    xmlns:mstns="example.com/rules-engine-configuration.xsd"
    xmlns:xs="example.com/2001/XMLSchema"
>
  <xs:include schemaLocation="RulesEngineSimpleTypes.xsd" />
  <xs:include schemaLocation="RulesEngineActivities.xsd" />
  <xs:include schemaLocation="RulesEngineAttributes.xsd" />
  <xs:include schemaLocation="RulesEngineCriteria.xsd" />
  <xs:include schemaLocation="RulesEngineEodSettings.xsd" />
  
  
  <!-- ===================== Common Simple Types ===================== -->
  <xs:simpleType name="EnvironmentType">
    <xs:annotation>
      <xs:documentation>A string of comma-separated values like RTA, PDS .</xs:documentation>
    </xs:annotation>

0 个答案:

没有答案