When attempting to validate your XML Document against a schema file in .NET, you might see the following error message:
[System.Xml.Schema.XmlSchemaException] = {"The targetNamespace parameter '' should be the same value as the targetNamespace 'urn:something' of the schema."}
Basically what this means is that either your schema file does not contain a “targetNamespace” attribute, or you haven’t specified the target namespace for the schema in your XmlSchemaSet.
Consider the following schema definition in file C:\schemas\testschema.xsd:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema elementFormDefault="qualified" targetNamespace="urn:something" xmlns="urn:something" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Document" type="Document"/> . . . </xs:schema>
Your code creates an XDocument:
XNamespace xmlns = XNamespace.Get("urn:something"); XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement(xmlns + "Document", // The root element. new XElement(xmlns + "ChildElement", . . . ) ) );
Then you add the schema for this namespace:
XmlSchemaSet schema = new XmlSchemaSet(); schema.Add(xmlns.ToString(), "C:\\schemas\\testschema.xsd");
And finally you validate your XDocument against the schema:
doc.Validate(schema, (o, e) => { Console.WriteLine(e.Message); });