I am using following code to validate my xml against xsd.
var isXmlValid = true;
var vinListMessage = "<root xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:test/properties/v1.0\"><test12121 id=\"3\"></test></root>";
var xsdFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "schema.xsd");
var schemas = new XmlSchemaSet();
schemas.Add(null, xsdFilePath);
var xmlDocument = XDocument.Parse(vinListMessage);
xmlDocument.Validate(schemas, (o, e) => { isXmlValid = false; });
Console.WriteLine(isXmlValid);
Please note the xmlns in the above xml, its urn:test/properties/v1.0
.
Now in my xsd I have targetnamespace
as targetNamespace="urn:testnew/properties/v1.0"
which is different than in the xml.
Now whatever xml I try to validate against the xsd it always return true. But If I match the namespaces then its working fine. I want to avoid dependency on namespace. Any suggestions?