Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Document Validation Error targetNamespace parameter

Tags:

c#

xml

I have a schema with the following attributes:

<xs:schema id="FooFile"  
    xmlns:xs="http://www.w3.org/2001/XMLSchema"  
    targetNamespace="http://Foostandards.com"    
    elementFormDefault="qualified"    
    xmlns="http://Foostandards.com">

I have an XDocument constructor with the following attributes on the root tag (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

I get the following error logged when I run the XDocument Validate method:

"The targetNamespace parameter '' should be the same value as the targetNamespace 'http://Foostandards.com' of the schema."

I have the targetNamespace parameter in the Schema and I can't find info that tells me that it even belongs in the XML document attributes (or how to code it).

like image 333
John Donnelly Avatar asked May 12 '14 17:05

John Donnelly


Video Answer


1 Answers

I figured it out. The error had nothing to do with the schema or the XDocument parameters. It was the Add method to the SchemaSet object that had a null value for the targetNamespace parameter.

My code:

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsd)));

Where xsd is a string representation of my schema. Note the "" as the first parameter of the Add method.

Code should have been:

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://Foostandards.com", XmlReader.Create(new StringReader(xsd)));
like image 147
John Donnelly Avatar answered Nov 10 '22 04:11

John Donnelly