Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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/9/javascript/463.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# 如何使用名称空间验证XML_C#_Xml_Validation_Xsd - Fatal编程技术网

C# 如何使用名称空间验证XML

C# 如何使用名称空间验证XML,c#,xml,validation,xsd,C#,Xml,Validation,Xsd,我试图理解如何验证一个包含多个名称空间的XML文档,但并没有走多远。作为我正在做的一个简化示例,我在一个名称空间中定义了一个“root”元素,如下所示: <root xmlns="root.xsd"> <child xmlns="child.xsd"/> </root> static void Main(string[] args) { using (var stream = new StreamReader("Example.xml"))

我试图理解如何验证一个包含多个名称空间的XML文档,但并没有走多远。作为我正在做的一个简化示例,我在一个名称空间中定义了一个“root”元素,如下所示:

<root xmlns="root.xsd">
    <child xmlns="child.xsd"/>
</root>
static void Main(string[] args)
{
    using (var stream = new StreamReader("Example.xml"))
    {
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += MyValidationEventHandler;
        schemaSet.XmlResolver = new XmlUrlResolver();

        schemaSet.Add(null, "root.xsd");

        XmlReaderSettings settings = new XmlReaderSettings()
        {
            ValidationType = ValidationType.Schema,
            ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
            Schemas = schemaSet,
        };

        settings.ValidationEventHandler += MyValidationEventHandler;

        XmlReader reader = XmlReader.Create(stream, settings);
        while (reader.Read())
        {
        }
    }
}

static void MyValidationEventHandler(object sender, ValidationEventArgs e)
{
}
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    targetNamespace="http://root.schemas.mycompany.com">

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    targetNamespace="http://child.schemas.mycompany.com">

<root xmlns="http://root.schemas.mycompany.com">
    <child xmlns="http://child.schemas.mycompany.com"/>
</root>
文件:“root.xsd”

但这给了我以下验证错误:

Could not find schema information for the element 'root.xsd:root'.
Could not find schema information for the element 'child.xsd:child'.
我希望XmlUrlResolver会找到xsd文件(保存在同一个文件夹中),但我猜它不会这么做


我的目标是拥有多个child.xsd类型的文件,这些文件在编译时之后生成,并且仅在运行时解析。这可能吗?

您需要这样的东西:

<root xmlns="root.xsd">
    <child xmlns="child.xsd"/>
</root>
static void Main(string[] args)
{
    using (var stream = new StreamReader("Example.xml"))
    {
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += MyValidationEventHandler;
        schemaSet.XmlResolver = new XmlUrlResolver();

        schemaSet.Add(null, "root.xsd");

        XmlReaderSettings settings = new XmlReaderSettings()
        {
            ValidationType = ValidationType.Schema,
            ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
            Schemas = schemaSet,
        };

        settings.ValidationEventHandler += MyValidationEventHandler;

        XmlReader reader = XmlReader.Create(stream, settings);
        while (reader.Read())
        {
        }
    }
}

static void MyValidationEventHandler(object sender, ValidationEventArgs e)
{
}
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    targetNamespace="http://root.schemas.mycompany.com">

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    targetNamespace="http://child.schemas.mycompany.com">

<root xmlns="http://root.schemas.mycompany.com">
    <child xmlns="http://child.schemas.mycompany.com"/>
</root>


您将XSD文件名视为命名空间名称,这是不正确的。此外,由于您没有使用
targetNamespace
,因此您的XML类型和元素根本不属于任何名称空间。

您需要在模式中使用
targetNamespace
。这些名称空间是您应该在
xmlns
中引用的。我的子架构的根架构包含一个include,它现在给了我一个错误:包含的架构的目标名称空间“”必须不存在或与包含架构的值“”匹配。我没有注意到
包含
,但这并不重要。事实上,您必须决定哪些类型和元素位于哪个名称空间中,然后您的XML必须引用正确的名称空间。请注意,这不是一个.NET问题-使用XMLspy或任何其他XML验证器都会出现相同的错误。。。我将include改为import,这样看起来效果更好。