XML文档验证错误targetNamespace参数

Joh*_*lly 6 c# xml

我有一个具有以下属性的架构:

<xs:schema id="FooFile"  
    xmlns:xs="http://www.w3.org/2001/XMLSchema"  
    targetNamespace="http://Foostandards.com"    
    elementFormDefault="qualified"    
    xmlns="http://Foostandards.com">
Run Code Online (Sandbox Code Playgroud)

我有一个XDocument构造函数,在根标记(FooFile)上具有以下属性.

XDocument Foo2Xml = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XComment("Foo2 file specifications implemented in xml"),
        new XElement("FooFile",
        new XAttribute(XNamespace.Xmlns + "xsi", "http://Foostandards.com"),
        new XAttribute(xsi + "schemaLocation", "http://Foostandards.com FooFile.xsd"), etc
Run Code Online (Sandbox Code Playgroud)

我运行XDocument Validate方法时记录以下错误:

"targetNamespace参数''应与模式的targetNamespace'http://Foostandards.com ' 相同."

我在Schema中有targetNamespace参数,我找不到告诉我它甚至属于XML文档属性(或如何编码)的信息.

Joh*_*lly 13

我想到了.该错误与架构或XDocument参数无关.SchemaSet对象的Add方法具有targetNamespace参数的空值.

我的代码:

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsd)));
Run Code Online (Sandbox Code Playgroud)

其中xsd是我的架构的字符串表示.请注意""作为Add方法的第一个参数.

代码应该是:

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://Foostandards.com", XmlReader.Create(new StringReader(xsd)));
Run Code Online (Sandbox Code Playgroud)